# Replication code for Taylor C. Boas, Dino P. Christenson, and David M. Glick, "Recruiting Large Online Samples in the United States and India: Facebook, Mechanical Turk and Qualtrics," Political Science Research and Methods.

# Analysis conducted in R 3.4.3 on MacOS 10.13.2

# NOTE: This file produces figures and tables related to politics: Main Text Figures 4-7 and Appendix Tables 5, 6, 10, and 11. Files should be run in the following order; please see readme.txt for details.
# 	1. clean_us_survey.R
# 	2. clean_india_survey.R
# 	3. merge_external_data_us.R
# 	4. merge_external_data_india.R
# 	5. analyze_demographics.R
# 	6. analyze_spaces.R
# 	7. analyze_politics.R
# 	8. analyze_cooperativeness.R
# 	9. analyze_experiments.R

# Set working directory as appropriate
# setwd('~/Dropbox/sample recruitment shared/replication/')

# Clean desktop and load packages. Please make sure all necessary packages are installed.
rm(list=ls(all=T))
library(Hmisc)
library(foreign)
library(survey)

# Load survey data. For Indian surveys other than WVS, we only have the marginals; using those to do t-tests. Relevant documents are in the main folder.

load('india_completions_augmented.RData')
load('us_completions_augmented.RData')
load('gss.RData')
load('anes.RData')
load('wvs.RData')

# US sample for unweighted analysis: excluding targeted respondents in Facebook/MTurk samples 

us1$nontarg<-((us1$sample=='Qualtrics')|(us1$sample=='Facebook'&us1$targ==0)|(us1$sample=='MTurk'&us1$StartDate <= as.POSIXct('2015-12-31 23:59:59 EST')))

###############################
# Appendix Table 5: US Politics
###############################

# Generate stacked datasets and weighted design objects for each comparison

gss$Q5<-gss$polviews
gss$dem_rep<-ifelse(gss$partyid==7,NA,gss$partyid+1)
gss$otherparty<-gss$partyid==7
us1$dem_rep<-1*(us1$Q9%in%1&us1$Q10%in%1)+2*(us1$Q9%in%1&us1$Q10%in%2)+3*(us1$Q9%in%3&us1$Q10_ind%in%3)+4*(us1$Q9%in%3&us1$Q10_ind%in%2)+5*(us1$Q9%in%3&us1$Q10_ind%in%1)+6*(us1$Q9%in%2&us1$Q10%in%2)+7*(us1$Q9%in%2&us1$Q10%in%1)
us1$dem_rep[us1$dem_rep==0]<-NA
us1$otherparty<-us1$Q9==4
gss$interest<- -1*gss$polint1+5 # Reversing scale, higher is more interested
us1$interest<- -1*us1$Q4+5
gss$voted<-ifelse(gss$vote12==3, NA, gss$vote12==1)
us1$voted<-ifelse(us1$Q7==3, NA, us1$Q7==1)
gss$voted_obama<-gss$pres12==1
gss$voted_romney<-gss$pres12==2
gss$voted_other<-gss$pres12==3
us1$voted_obama<-us1$Q8==1
us1$voted_romney<-us1$Q8==2
us1$voted_other<-us1$Q8==3

anes$internet_news<-ifelse(unclass(anes$prmedia_wkinews) < 7, NA, unclass(anes$prmedia_wkinews) - 7)
anes$tv_news<-ifelse(unclass(anes$prmedia_wktvnws) < 7, NA, unclass(anes$prmedia_wktvnws) - 7)
anes$radio_news<-ifelse(unclass(anes$prmedia_wkrdnws) < 7, NA, unclass(anes$prmedia_wkrdnws) - 7)
anes$paper_news<-ifelse(unclass(anes$prmedia_wkpaprnws) < 7, NA, unclass(anes$prmedia_wkpaprnws) - 7)
us1$paper_news<-us1$Q12a-1
us1$radio_news<-us1$Q12b-1
us1$internet_news<-us1$Q12c-1
us1$tv_news<-us1$Q12d-1
anes$Q13_correct<-ifelse(anes$preknow_prestimes < 0, NA, anes$preknow_prestimes==2)
anes$Q14_correct<-ifelse(anes$preknow_senterm < 0, NA, anes$preknow_senterm ==6)

anes$Q15_correct<-0
anes$Q15_correct[grep('prime minister|prime minitry|head of parliament|prime ministy|priminister|parliament\\. head|england prie mi izf|prime ministry|prime minster|mp of england|pm of great britain|pm of great britain|prime iminsiter|prime miister|prime ministry|prime minster|prime minstry|prome miinstry|uk pm', tolower(anes$ofcrec_pmuk_txt))]<-1
anes$Q15_correct[grep('diplomat|^ex', tolower(anes$ofcrec_pmuk_txt))]<-0

anes$Q16_correct<-0
anes$Q16_correct[grep('justice|supreme court|judge|supreeme  justic|supreme cor=urt|surpreme court', tolower(anes$ofcrec_cj_txt))]<-1
anes$Q16_correct[grep('district court judge', tolower(anes$ofcrec_cj_txt))]<-0

anes$Q17_correct<-unclass(anes$preknow_medicare)==8
us1$Q17_correct<-us1$Q17==1
anes$registered<-ifelse(unclass(anes$prevote_regist_addr) < 7, NA, unclass(anes$prevote_regist_addr) %in% 7:8)
us1$registered<-us1$Q6==1

us.fb.gss<-rbind(us1[us1$sample=='Facebook',c('sample','weight','nontarg','Q5','dem_rep','otherparty','interest','voted','voted_obama','voted_romney','voted_other')],data.frame(sample=' GSS', weight=1, nontarg=T, gss[,c('Q5','dem_rep','otherparty','interest','voted','voted_obama','voted_romney','voted_other')]))
us.fb.gss.design<-svydesign(ids=~1,weights= us.fb.gss$weight,data= us.fb.gss)
us.mt.gss<-rbind(us1[us1$sample=='MTurk',c('sample','weight','nontarg','Q5','dem_rep','otherparty','interest','voted','voted_obama','voted_romney','voted_other')],data.frame(sample=' GSS', weight=1, nontarg=T, gss[,c('Q5','dem_rep','otherparty','interest','voted','voted_obama','voted_romney','voted_other')]))
us.mt.gss.design<-svydesign(ids=~1,weights= us.mt.gss$weight,data= us.mt.gss)
us.qt.gss<-rbind(us1[us1$sample=='Qualtrics',c('sample','weight','nontarg','Q5','dem_rep','otherparty','interest','voted','voted_obama','voted_romney','voted_other')],data.frame(sample=' GSS', weight=1, nontarg=T, gss[,c('Q5','dem_rep','otherparty','interest','voted','voted_obama','voted_romney','voted_other')]))
us.qt.gss.design<-svydesign(ids=~1,weights= us.qt.gss$weight,data= us.qt.gss)

us.fb.anes<-rbind(us1[us1$sample=='Facebook',c('sample','weight','nontarg','internet_news','tv_news','radio_news','paper_news','Q13_correct','Q14_correct','Q15_correct','Q16_correct', 'Q17_correct','registered')],data.frame(sample=' ANES', weight=1, nontarg=T, anes[,c('internet_news','tv_news','radio_news','paper_news','Q13_correct','Q14_correct','Q15_correct','Q16_correct', 'Q17_correct','registered')]))
us.fb.anes.design<-svydesign(ids=~1,weights= us.fb.anes$weight,data= us.fb.anes)
us.mt.anes<-rbind(us1[us1$sample=='MTurk',c('sample','weight','nontarg','internet_news','tv_news','radio_news','paper_news','Q13_correct','Q14_correct','Q15_correct','Q16_correct', 'Q17_correct','registered')],data.frame(sample=' ANES', weight=1, nontarg=T, anes[,c('internet_news','tv_news','radio_news','paper_news','Q13_correct','Q14_correct','Q15_correct','Q16_correct', 'Q17_correct','registered')]))
us.mt.anes.design<-svydesign(ids=~1,weights= us.mt.anes$weight,data= us.mt.anes)
us.qt.anes<-rbind(us1[us1$sample=='Qualtrics',c('sample','weight','nontarg','internet_news','tv_news','radio_news','paper_news','Q13_correct','Q14_correct','Q15_correct','Q16_correct', 'Q17_correct','registered')],data.frame(sample=' ANES', weight=1, nontarg=T, anes[,c('internet_news','tv_news','radio_news','paper_news','Q13_correct','Q14_correct','Q15_correct','Q16_correct', 'Q17_correct','registered')]))
us.qt.anes.design<-svydesign(ids=~1,weights= us.qt.anes$weight,data= us.qt.anes)

# Ideology
reg.us.ideol.fb<-with(us.fb.gss[us.fb.gss$nontarg,],lm(Q5~sample))
reg.us.ideol.fb.wtd<-svyglm(Q5~sample, design= us.fb.gss.design)
reg.us.ideol.mt<-with(us.mt.gss[us.mt.gss$nontarg,],lm(Q5~sample))
reg.us.ideol.mt.wtd<-svyglm(Q5~sample, design= us.mt.gss.design)
reg.us.ideol.qt<-with(us.qt.gss[us.qt.gss$nontarg,],lm(Q5~sample))
reg.us.ideol.qt.wtd<-svyglm(Q5~sample, design= us.qt.gss.design)

# Partisanship
reg.us.dem_rep.fb<-with(us.fb.gss[us.fb.gss$nontarg,],lm(dem_rep~sample))
reg.us.dem_rep.fb.wtd<-svyglm(dem_rep~sample, design= us.fb.gss.design)
reg.us.dem_rep.mt<-with(us.mt.gss[us.mt.gss$nontarg,],lm(dem_rep~sample))
reg.us.dem_rep.mt.wtd<-svyglm(dem_rep~sample, design= us.mt.gss.design)
reg.us.dem_rep.qt<-with(us.qt.gss[us.qt.gss$nontarg,],lm(dem_rep~sample))
reg.us.dem_rep.qt.wtd<-svyglm(dem_rep~sample, design= us.qt.gss.design)

# Interest
reg.us.interest.fb<-with(us.fb.gss[us.fb.gss$nontarg,],lm(interest~sample))
reg.us.interest.fb.wtd<-svyglm(interest~sample, design= us.fb.gss.design)
reg.us.interest.mt<-with(us.mt.gss[us.mt.gss$nontarg,],lm(interest~sample))
reg.us.interest.mt.wtd<-svyglm(interest~sample, design= us.mt.gss.design)
reg.us.interest.qt<-with(us.qt.gss[us.qt.gss$nontarg,],lm(interest~sample))
reg.us.interest.qt.wtd<-svyglm(interest~sample, design= us.qt.gss.design)

# Internet news
reg.us.internet_news.fb<-with(us.fb.anes[us.fb.anes$nontarg,],lm(internet_news~sample))
reg.us.internet_news.fb.wtd<-svyglm(internet_news~sample, design= us.fb.anes.design)
reg.us.internet_news.mt<-with(us.mt.anes[us.mt.anes$nontarg,],lm(internet_news~sample))
reg.us.internet_news.mt.wtd<-svyglm(internet_news~sample, design= us.mt.anes.design)
reg.us.internet_news.qt<-with(us.qt.anes[us.qt.anes$nontarg,],lm(internet_news~sample))
reg.us.internet_news.qt.wtd<-svyglm(internet_news~sample, design= us.qt.anes.design)

# TV news
reg.us.tv_news.fb<-with(us.fb.anes[us.fb.anes$nontarg,],lm(tv_news~sample))
reg.us.tv_news.fb.wtd<-svyglm(tv_news~sample, design= us.fb.anes.design)
reg.us.tv_news.mt<-with(us.mt.anes[us.mt.anes$nontarg,],lm(tv_news~sample))
reg.us.tv_news.mt.wtd<-svyglm(tv_news~sample, design= us.mt.anes.design)
reg.us.tv_news.qt<-with(us.qt.anes[us.qt.anes$nontarg,],lm(tv_news~sample))
reg.us.tv_news.qt.wtd<-svyglm(tv_news~sample, design= us.qt.anes.design)

# Radio news
reg.us.radio_news.fb<-with(us.fb.anes[us.fb.anes$nontarg,],lm(radio_news~sample))
reg.us.radio_news.fb.wtd<-svyglm(radio_news~sample, design= us.fb.anes.design)
reg.us.radio_news.mt<-with(us.mt.anes[us.mt.anes$nontarg,],lm(radio_news~sample))
reg.us.radio_news.mt.wtd<-svyglm(radio_news~sample, design= us.mt.anes.design)
reg.us.radio_news.qt<-with(us.qt.anes[us.qt.anes$nontarg,],lm(radio_news~sample))
reg.us.radio_news.qt.wtd<-svyglm(radio_news~sample, design= us.qt.anes.design)

# Paper news
reg.us.paper_news.fb<-with(us.fb.anes[us.fb.anes$nontarg,],lm(paper_news~sample))
reg.us.paper_news.fb.wtd<-svyglm(paper_news~sample, design= us.fb.anes.design)
reg.us.paper_news.mt<-with(us.mt.anes[us.mt.anes$nontarg,],lm(paper_news~sample))
reg.us.paper_news.mt.wtd<-svyglm(paper_news~sample, design= us.mt.anes.design)
reg.us.paper_news.qt<-with(us.qt.anes[us.qt.anes$nontarg,],lm(paper_news~sample))
reg.us.paper_news.qt.wtd<-svyglm(paper_news~sample, design= us.qt.anes.design)

# Know presidential term limit
reg.us.know_reelec.fb<-with(us.fb.anes[us.fb.anes$nontarg,],lm(Q13_correct~sample))
reg.us.know_reelec.fb.wtd<-svyglm(Q13_correct~sample, design= us.fb.anes.design)
reg.us.know_reelec.mt<-with(us.mt.anes[us.mt.anes$nontarg,],lm(Q13_correct~sample))
reg.us.know_reelec.mt.wtd<-svyglm(Q13_correct~sample, design= us.mt.anes.design)
reg.us.know_reelec.qt<-with(us.qt.anes[us.qt.anes$nontarg,],lm(Q13_correct~sample))
reg.us.know_reelec.qt.wtd<-svyglm(Q13_correct~sample, design= us.qt.anes.design)

# Know length of a senate term
reg.us.know_senate.fb<-with(us.fb.anes[us.fb.anes$nontarg,],lm(Q14_correct~sample))
reg.us.know_senate.fb.wtd<-svyglm(Q14_correct~sample, design= us.fb.anes.design)
reg.us.know_senate.mt<-with(us.mt.anes[us.mt.anes$nontarg,],lm(Q14_correct~sample))
reg.us.know_senate.mt.wtd<-svyglm(Q14_correct~sample, design= us.mt.anes.design)
reg.us.know_senate.qt<-with(us.qt.anes[us.qt.anes$nontarg,],lm(Q14_correct~sample))
reg.us.know_senate.qt.wtd<-svyglm(Q14_correct~sample, design= us.qt.anes.design)

# Know David Cameron office
reg.us.know_cameron.fb<-with(us.fb.anes[us.fb.anes$nontarg,],lm(Q15_correct~sample))
reg.us.know_cameron.fb.wtd<-svyglm(Q15_correct~sample, design= us.fb.anes.design)
reg.us.know_cameron.mt<-with(us.mt.anes[us.mt.anes$nontarg,],lm(Q15_correct~sample))
reg.us.know_cameron.mt.wtd<-svyglm(Q15_correct~sample, design= us.mt.anes.design)
reg.us.know_cameron.qt<-with(us.qt.anes[us.qt.anes$nontarg,],lm(Q15_correct~sample))
reg.us.know_cameron.qt.wtd<-svyglm(Q15_correct~sample, design= us.qt.anes.design)

# Know John Roberts office
reg.us.know_roberts.fb<-with(us.fb.anes[us.fb.anes$nontarg,],lm(Q16_correct~sample))
reg.us.know_roberts.fb.wtd<-svyglm(Q16_correct~sample, design= us.fb.anes.design)
reg.us.know_roberts.mt<-with(us.mt.anes[us.mt.anes$nontarg,],lm(Q16_correct~sample))
reg.us.know_roberts.mt.wtd<-svyglm(Q16_correct~sample, design= us.mt.anes.design)
reg.us.know_roberts.qt<-with(us.qt.anes[us.qt.anes$nontarg,],lm(Q16_correct~sample))
reg.us.know_roberts.qt.wtd<-svyglm(Q16_correct~sample, design= us.qt.anes.design)

# Know Medicare
reg.us.know_medicare.fb<-with(us.fb.anes[us.fb.anes$nontarg,],lm(Q17_correct~sample))
reg.us.know_medicare.fb.wtd<-svyglm(Q17_correct~sample, design= us.fb.anes.design)
reg.us.know_medicare.mt<-with(us.mt.anes[us.mt.anes$nontarg,],lm(Q17_correct~sample))
reg.us.know_medicare.mt.wtd<-svyglm(Q17_correct~sample, design= us.mt.anes.design)
reg.us.know_medicare.qt<-with(us.qt.anes[us.qt.anes$nontarg,],lm(Q17_correct~sample))
reg.us.know_medicare.qt.wtd<-svyglm(Q17_correct~sample, design= us.qt.anes.design)

# Registered
reg.us.registered.fb<-with(us.fb.anes[us.fb.anes$nontarg,],lm(registered~sample))
reg.us.registered.fb.wtd<-svyglm(registered~sample, design= us.fb.anes.design)
reg.us.registered.mt<-with(us.mt.anes[us.mt.anes$nontarg,],lm(registered~sample))
reg.us.registered.mt.wtd<-svyglm(registered~sample, design= us.mt.anes.design)
reg.us.registered.qt<-with(us.qt.anes[us.qt.anes$nontarg,],lm(registered~sample))
reg.us.registered.qt.wtd<-svyglm(registered~sample, design= us.qt.anes.design)

# Voted
reg.us.voted.fb<-with(us.fb.gss[us.fb.gss$nontarg,],lm(voted~sample))
reg.us.voted.fb.wtd<-svyglm(voted~sample, design= us.fb.gss.design)
reg.us.voted.mt<-with(us.mt.gss[us.mt.gss$nontarg,],lm(voted~sample))
reg.us.voted.mt.wtd<-svyglm(voted~sample, design= us.mt.gss.design)
reg.us.voted.qt<-with(us.qt.gss[us.qt.gss$nontarg,],lm(voted~sample))
reg.us.voted.qt.wtd<-svyglm(voted~sample, design= us.qt.gss.design)

# Voted Obama
reg.us.voted_obama.fb<-with(us.fb.gss[us.fb.gss$nontarg,],lm(voted_obama~sample))
reg.us.voted_obama.fb.wtd<-svyglm(voted_obama~sample, design= us.fb.gss.design)
reg.us.voted_obama.mt<-with(us.mt.gss[us.mt.gss$nontarg,],lm(voted_obama~sample))
reg.us.voted_obama.mt.wtd<-svyglm(voted_obama~sample, design= us.mt.gss.design)
reg.us.voted_obama.qt<-with(us.qt.gss[us.qt.gss$nontarg,],lm(voted_obama~sample))
reg.us.voted_obama.qt.wtd<-svyglm(voted_obama~sample, design= us.qt.gss.design)

# Voted Romney
reg.us.voted_romney.fb<-with(us.fb.gss[us.fb.gss$nontarg,],lm(voted_romney~sample))
reg.us.voted_romney.fb.wtd<-svyglm(voted_romney~sample, design= us.fb.gss.design)
reg.us.voted_romney.mt<-with(us.mt.gss[us.mt.gss$nontarg,],lm(voted_romney~sample))
reg.us.voted_romney.mt.wtd<-svyglm(voted_romney~sample, design= us.mt.gss.design)
reg.us.voted_romney.qt<-with(us.qt.gss[us.qt.gss$nontarg,],lm(voted_romney~sample))
reg.us.voted_romney.qt.wtd<-svyglm(voted_romney~sample, design= us.qt.gss.design)

# Voted other
reg.us.voted_other.fb<-with(us.fb.gss[us.fb.gss$nontarg,],lm(voted_other~sample))
reg.us.voted_other.fb.wtd<-svyglm(voted_other~sample, design= us.fb.gss.design)
reg.us.voted_other.mt<-with(us.mt.gss[us.mt.gss$nontarg,],lm(voted_other~sample))
reg.us.voted_other.mt.wtd<-svyglm(voted_other~sample, design= us.mt.gss.design)
reg.us.voted_other.qt<-with(us.qt.gss[us.qt.gss$nontarg,],lm(voted_other~sample))
reg.us.voted_other.qt.wtd<-svyglm(voted_other~sample, design= us.qt.gss.design)

# Gather coefficients for table

pval<-function(reg) ifelse(nrow(coef(summary(reg)))==1,NA,coef(summary(reg))[2,4])

stars<-function(pval){
	stars<-1*(pval < 0.1) +1*(pval < 0.05) + 1*(pval < 0.01) + 1*(pval < 0.001)
	stars[is.na(stars)]<-0
	stars<-as.character(factor(stars,levels=0:4,labels=c('','$\\dagger$','*','**','***')))
	stars
}

est.us.prob<-round(sapply(list(reg.us.ideol.fb, reg.us.dem_rep.fb, reg.us.interest.fb, reg.us.registered.fb, reg.us.voted.fb, reg.us.voted_obama.fb, reg.us.voted_romney.fb, reg.us.voted_other.fb, reg.us.internet_news.fb, reg.us.tv_news.fb, reg.us.radio_news.fb, reg.us.paper_news.fb, reg.us.know_reelec.fb, reg.us.know_senate.fb, reg.us.know_cameron.fb, reg.us.know_roberts.fb, reg.us.know_medicare.fb), function(x) coef(x)[1]),2)
est.us.fb<-round(sapply(list(reg.us.ideol.fb, reg.us.dem_rep.fb, reg.us.interest.fb, reg.us.registered.fb, reg.us.voted.fb, reg.us.voted_obama.fb, reg.us.voted_romney.fb, reg.us.voted_other.fb, reg.us.internet_news.fb, reg.us.tv_news.fb, reg.us.radio_news.fb, reg.us.paper_news.fb, reg.us.know_reelec.fb, reg.us.know_senate.fb, reg.us.know_cameron.fb, reg.us.know_roberts.fb, reg.us.know_medicare.fb), function(x) sum(coef(x))),2)
pval.us.fb<-sapply(list(reg.us.ideol.fb, reg.us.dem_rep.fb, reg.us.interest.fb, reg.us.registered.fb, reg.us.voted.fb, reg.us.voted_obama.fb, reg.us.voted_romney.fb, reg.us.voted_other.fb, reg.us.internet_news.fb, reg.us.tv_news.fb, reg.us.radio_news.fb, reg.us.paper_news.fb, reg.us.know_reelec.fb, reg.us.know_senate.fb, reg.us.know_cameron.fb, reg.us.know_roberts.fb, reg.us.know_medicare.fb), pval)
est.us.fb.wtd<-round(sapply(list(reg.us.ideol.fb.wtd, reg.us.dem_rep.fb.wtd, reg.us.interest.fb.wtd, reg.us.registered.fb.wtd, reg.us.voted.fb.wtd, reg.us.voted_obama.fb.wtd, reg.us.voted_romney.fb.wtd, reg.us.voted_other.fb.wtd, reg.us.internet_news.fb.wtd, reg.us.tv_news.fb.wtd, reg.us.radio_news.fb.wtd, reg.us.paper_news.fb.wtd, reg.us.know_reelec.fb.wtd, reg.us.know_senate.fb.wtd, reg.us.know_cameron.fb.wtd, reg.us.know_roberts.fb.wtd, reg.us.know_medicare.fb.wtd), function(x) sum(coef(x))),2)
pval.us.fb.wtd<-sapply(list(reg.us.ideol.fb.wtd, reg.us.dem_rep.fb.wtd, reg.us.interest.fb.wtd, reg.us.registered.fb.wtd, reg.us.voted.fb.wtd, reg.us.voted_obama.fb.wtd, reg.us.voted_romney.fb.wtd, reg.us.voted_other.fb.wtd, reg.us.internet_news.fb.wtd, reg.us.tv_news.fb.wtd, reg.us.radio_news.fb.wtd, reg.us.paper_news.fb.wtd, reg.us.know_reelec.fb.wtd, reg.us.know_senate.fb.wtd, reg.us.know_cameron.fb.wtd, reg.us.know_roberts.fb.wtd, reg.us.know_medicare.fb.wtd), pval)
est.us.mt<-round(sapply(list(reg.us.ideol.mt, reg.us.dem_rep.mt, reg.us.interest.mt, reg.us.registered.mt, reg.us.voted.mt, reg.us.voted_obama.mt, reg.us.voted_romney.mt, reg.us.voted_other.mt, reg.us.internet_news.mt, reg.us.tv_news.mt, reg.us.radio_news.mt, reg.us.paper_news.mt, reg.us.know_reelec.mt, reg.us.know_senate.mt, reg.us.know_cameron.mt, reg.us.know_roberts.mt, reg.us.know_medicare.mt), function(x) sum(coef(x))),2)
pval.us.mt<-sapply(list(reg.us.ideol.mt, reg.us.dem_rep.mt, reg.us.interest.mt, reg.us.registered.mt, reg.us.voted.mt, reg.us.voted_obama.mt, reg.us.voted_romney.mt, reg.us.voted_other.mt, reg.us.internet_news.mt, reg.us.tv_news.mt, reg.us.radio_news.mt, reg.us.paper_news.mt, reg.us.know_reelec.mt, reg.us.know_senate.mt, reg.us.know_cameron.mt, reg.us.know_roberts.mt, reg.us.know_medicare.mt), pval)
est.us.mt.wtd<-round(sapply(list(reg.us.ideol.mt.wtd, reg.us.dem_rep.mt.wtd, reg.us.interest.mt.wtd, reg.us.registered.mt.wtd, reg.us.voted.mt.wtd, reg.us.voted_obama.mt.wtd, reg.us.voted_romney.mt.wtd, reg.us.voted_other.mt.wtd, reg.us.internet_news.mt.wtd, reg.us.tv_news.mt.wtd, reg.us.radio_news.mt.wtd, reg.us.paper_news.mt.wtd, reg.us.know_reelec.mt.wtd, reg.us.know_senate.mt.wtd, reg.us.know_cameron.mt.wtd, reg.us.know_roberts.mt.wtd, reg.us.know_medicare.mt.wtd), function(x) sum(coef(x))),2)
pval.us.mt.wtd<-sapply(list(reg.us.ideol.mt.wtd, reg.us.dem_rep.mt.wtd, reg.us.interest.mt.wtd, reg.us.registered.mt.wtd, reg.us.voted.mt.wtd, reg.us.voted_obama.mt.wtd, reg.us.voted_romney.mt.wtd, reg.us.voted_other.mt.wtd, reg.us.internet_news.mt.wtd, reg.us.tv_news.mt.wtd, reg.us.radio_news.mt.wtd, reg.us.paper_news.mt.wtd, reg.us.know_reelec.mt.wtd, reg.us.know_senate.mt.wtd, reg.us.know_cameron.mt.wtd, reg.us.know_roberts.mt.wtd, reg.us.know_medicare.mt.wtd), pval)
est.us.qt<-round(sapply(list(reg.us.ideol.qt, reg.us.dem_rep.qt, reg.us.interest.qt, reg.us.registered.qt, reg.us.voted.qt, reg.us.voted_obama.qt, reg.us.voted_romney.qt, reg.us.voted_other.qt, reg.us.internet_news.qt, reg.us.tv_news.qt, reg.us.radio_news.qt, reg.us.paper_news.qt, reg.us.know_reelec.qt, reg.us.know_senate.qt, reg.us.know_cameron.qt, reg.us.know_roberts.qt, reg.us.know_medicare.qt), function(x) sum(coef(x))),2)
pval.us.qt<-sapply(list(reg.us.ideol.qt, reg.us.dem_rep.qt, reg.us.interest.qt, reg.us.registered.qt, reg.us.voted.qt, reg.us.voted_obama.qt, reg.us.voted_romney.qt, reg.us.voted_other.qt, reg.us.internet_news.qt, reg.us.tv_news.qt, reg.us.radio_news.qt, reg.us.paper_news.qt, reg.us.know_reelec.qt, reg.us.know_senate.qt, reg.us.know_cameron.qt, reg.us.know_roberts.qt, reg.us.know_medicare.qt), pval)
est.us.qt.wtd<-round(sapply(list(reg.us.ideol.qt.wtd, reg.us.dem_rep.qt.wtd, reg.us.interest.qt.wtd, reg.us.registered.qt.wtd, reg.us.voted.qt.wtd, reg.us.voted_obama.qt.wtd, reg.us.voted_romney.qt.wtd, reg.us.voted_other.qt.wtd, reg.us.internet_news.qt.wtd, reg.us.tv_news.qt.wtd, reg.us.radio_news.qt.wtd, reg.us.paper_news.qt.wtd, reg.us.know_reelec.qt.wtd, reg.us.know_senate.qt.wtd, reg.us.know_cameron.qt.wtd, reg.us.know_roberts.qt.wtd, reg.us.know_medicare.qt.wtd), function(x) sum(coef(x))),2)
pval.us.qt.wtd<-sapply(list(reg.us.ideol.qt.wtd, reg.us.dem_rep.qt.wtd, reg.us.interest.qt.wtd, reg.us.registered.qt.wtd, reg.us.voted.qt.wtd, reg.us.voted_obama.qt.wtd, reg.us.voted_romney.qt.wtd, reg.us.voted_other.qt.wtd, reg.us.internet_news.qt.wtd, reg.us.tv_news.qt.wtd, reg.us.radio_news.qt.wtd, reg.us.paper_news.qt.wtd, reg.us.know_reelec.qt.wtd, reg.us.know_senate.qt.wtd, reg.us.know_cameron.qt.wtd, reg.us.know_roberts.qt.wtd, reg.us.know_medicare.qt.wtd), pval)

pval.adj.us<-p.adjust(c(pval.us.fb, pval.us.fb.wtd, pval.us.mt, pval.us.mt.wtd, pval.us.qt, pval.us.qt.wtd),method='BH')
stars.us<-data.frame(matrix(stars(pval.adj.us),nrow=length(est.us.prob)))
names(stars.us)<-c('fb','fb.wtd','mt','mt.wtd','qt','qt.wtd')

us.pol.table<-data.frame(prob=as.character(est.us.prob),fb=paste(est.us.fb,stars.us$fb,sep=''), fb.wtd=paste(est.us.fb.wtd,stars.us$fb.wtd,sep=''), mt=paste(est.us.mt,stars.us$mt,sep=''), mt.wtd=paste(est.us.mt.wtd,stars.us$mt.wtd,sep=''), qt=paste(est.us.qt,stars.us$qt,sep=''), qt.wtd=paste(est.us.qt.wtd,stars.us$qt.wtd,sep=''),stringsAsFactors=F)
rownames(us.pol.table)<-c('Lib.--Con. (1--7)','Dem.--Repub. (1--7)','Political interest (1--4)','Registered to Vote','Turnout 2012','Obama','Romney','Other','Internet','Television','Radio','Newspaper','Pres. term limits','Senate term length','David Cameron','John Roberts','Medicare')
colnames(us.pol.table)<-c('Probability\nSample','Facebook','Facebook\nWeighted','MTurk','MTurk\nWeighted','Qualtrics','Qualtrics\nWeighted')

# Bold results that are consistent with hypotheses; underline results that go against.
us.hypo.table<-matrix(
	c(NA,T,NA,T,NA,NA,NA,
	NA,T,NA,T,NA,NA,NA,
	NA,NA,NA,NA,NA,NA,NA,
	NA,NA,NA,NA,NA,NA,NA,
	NA,NA,NA,NA,NA,NA,NA,
	NA,T,NA,T,NA,NA,NA,
	NA,NA,NA,NA,NA,NA,NA,
	NA,NA,NA,NA,NA,NA,NA,
	NA,T,NA,T,NA,NA,NA,
	NA,T,NA,F,NA,NA,NA,
	NA,T,NA,F,NA,NA,NA,
	NA,T,NA,F,NA,NA,NA,
	NA,T,NA,T,NA,NA,NA,
	NA,T,NA,T,NA,NA,NA,
	NA,T,NA,T,NA,NA,NA,
	NA,T,NA,T,NA,NA,NA,
	NA,T,NA,F,NA,NA,NA), byrow=T,nrow=length(est.us.prob))

for (i in 1:nrow(us.pol.table)){
	for (j in 1:ncol(us.pol.table)){
		if (is.na(us.hypo.table[i,j])) next
		us.pol.table[i,j]<-ifelse(us.hypo.table[i,j], paste0('\\textbf{', us.pol.table[i,j],'}'), paste0('\\underline{', us.pol.table[i,j],'}'))
	}
}

us.pol.table.note<-"NOTE: Entries are mean or weighted mean values for each sample. Probability sample is the 2012 American National Election Study (news, knowledge, and registration; non-oversampled face-to-face interviews only) or the 2014 General Social Survey cross-section (all other variables). Weights are post-stratification, based on region (4 categories), age (quartiles), and sex in the 2010 census. Stars report significance levels from two-tailed difference-in-means t-tests comparing the probability and convenience sample quantities, with adjustment for multiple comparisons using the Benjamini-Hochberg method (all hypotheses in the table are a single family). Results in bold type support pre-registered hypotheses, underlined results contradict pre-registered hypotheses, and those in regular font were not pre-registered. $\\dagger$ p$<$0.1, *p$<$0.05, **p$<$0.01, ***p$<$0.001."

us.pol.table.latex<-latex(us.pol.table,file='us_pol_table.tex', rowlabel = '', collabel.just=rep('c',ncol(us.pol.table)), col.just=rep('c',ncol(us.pol.table)), rgroup=c('General','2012 Presidential Vote','News Exposure (0--7)', 'Political Knowledge'), n.rgroup=c(5,3,4,5), caption = 'U.S. Political Variables: Convenience versus Probability Samples', extracolsize='normalsize', insert.bottom= us.pol.table.note, booktabs = F, ctable = T, where = "htp")

##########################################################
# Main Text Figures 4-5 and Appendix Table 10: US Politics
##########################################################

# Weighted design objects, convenience samples only

us.fb.design<-svydesign(ids=~1,weights= us1$weight[us1$sample=='Facebook'],data= us1[us1$sample=='Facebook',])
us.mt.design<-svydesign(ids=~1,weights= us1$weight[us1$sample=='MTurk'],data= us1[us1$sample=='MTurk',])
us.qt.design<-svydesign(ids=~1,weights= us1$weight[us1$sample=='Qualtrics'],data= us1[us1$sample=='Qualtrics',])

# Ideology
mean.us.ideol.gss<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample==' GSS',],lm(I((Q5-1)/6)~1))
mean.us.ideol.fb<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample=='Facebook',],lm(I((Q5-1)/6)~1))
mean.us.ideol.mt<-with(us.mt.gss[us.mt.gss$nontarg & us.mt.gss$sample=='MTurk',],lm(I((Q5-1)/6)~1))
mean.us.ideol.qt<-with(us.qt.gss[us.qt.gss$nontarg & us.qt.gss$sample=='Qualtrics',],lm(I((Q5-1)/6)~1))

mean.us.ideol.fb.wtd<-svyglm(I((Q5-1)/6)~1, design= us.fb.design)
mean.us.ideol.mt.wtd<-svyglm(I((Q5-1)/6)~1, design= us.mt.design)
mean.us.ideol.qt.wtd<-svyglm(I((Q5-1)/6)~1, design= us.qt.design)

sd.us.ideol.gss<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample==' GSS',],sd(I((Q5-1)/6),na.rm=T))

# Partisanship
mean.us.dem_rep.gss<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample==' GSS',],lm(I((dem_rep-1)/6)~1))
mean.us.dem_rep.fb<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample=='Facebook',],lm(I((dem_rep-1)/6)~1))
mean.us.dem_rep.mt<-with(us.mt.gss[us.mt.gss$nontarg & us.mt.gss$sample=='MTurk',],lm(I((dem_rep-1)/6)~1))
mean.us.dem_rep.qt<-with(us.qt.gss[us.qt.gss$nontarg & us.qt.gss$sample=='Qualtrics',],lm(I((dem_rep-1)/6)~1))

mean.us.dem_rep.fb.wtd<-svyglm(I((dem_rep-1)/6)~1, design= us.fb.design)
mean.us.dem_rep.mt.wtd<-svyglm(I((dem_rep-1)/6)~1, design= us.mt.design)
mean.us.dem_rep.qt.wtd<-svyglm(I((dem_rep-1)/6)~1, design= us.qt.design)

sd.us.dem_rep.gss<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample==' GSS',],sd(I((dem_rep-1)/6),na.rm=T))

# Interest
mean.us.interest.gss<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample==' GSS',],lm(I((interest-1)/3)~1))
mean.us.interest.fb<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample=='Facebook',],lm(I((interest-1)/3)~1))
mean.us.interest.mt<-with(us.mt.gss[us.mt.gss$nontarg & us.mt.gss$sample=='MTurk',],lm(I((interest-1)/3)~1))
mean.us.interest.qt<-with(us.qt.gss[us.qt.gss$nontarg & us.qt.gss$sample=='Qualtrics',],lm(I((interest-1)/3)~1))

mean.us.interest.fb.wtd<-svyglm(I((interest-1)/3)~1, design= us.fb.design)
mean.us.interest.mt.wtd<-svyglm(I((interest-1)/3)~1, design= us.mt.design)
mean.us.interest.qt.wtd<-svyglm(I((interest-1)/3)~1, design= us.qt.design)

sd.us.interest.gss<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample==' GSS',],sd(I((interest-1)/3),na.rm=T))

# Internet news
mean.us.internet_news.anes<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample==' ANES',],lm(I(internet_news/7)~1))
mean.us.internet_news.fb<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample=='Facebook',],lm(I(internet_news/7)~1))
mean.us.internet_news.mt<-with(us.mt.anes[us.mt.anes$nontarg & us.mt.anes$sample=='MTurk',],lm(I(internet_news/7)~1))
mean.us.internet_news.qt<-with(us.qt.anes[us.qt.anes$nontarg & us.qt.anes$sample=='Qualtrics',],lm(I(internet_news/7)~1))

mean.us.internet_news.fb.wtd<-svyglm(I(internet_news/7)~1, design= us.fb.design)
mean.us.internet_news.mt.wtd<-svyglm(I(internet_news/7)~1, design= us.mt.design)
mean.us.internet_news.qt.wtd<-svyglm(I(internet_news/7)~1, design= us.qt.design)

sd.us.internet_news.anes<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample==' ANES',],sd(I(internet_news/7),na.rm=T))

# TV news
mean.us.tv_news.anes<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample==' ANES',],lm(I(tv_news/7)~1))
mean.us.tv_news.fb<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample=='Facebook',],lm(I(tv_news/7)~1))
mean.us.tv_news.mt<-with(us.mt.anes[us.mt.anes$nontarg & us.mt.anes$sample=='MTurk',],lm(I(tv_news/7)~1))
mean.us.tv_news.qt<-with(us.qt.anes[us.qt.anes$nontarg & us.qt.anes$sample=='Qualtrics',],lm(I(tv_news/7)~1))

mean.us.tv_news.fb.wtd<-svyglm(I(tv_news/7)~1, design= us.fb.design)
mean.us.tv_news.mt.wtd<-svyglm(I(tv_news/7)~1, design= us.mt.design)
mean.us.tv_news.qt.wtd<-svyglm(I(tv_news/7)~1, design= us.qt.design)

sd.us.tv_news.anes<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample==' ANES',],sd(I(tv_news/7),na.rm=T))

# Radio news
mean.us.radio_news.anes<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample==' ANES',],lm(I(radio_news/7)~1))
mean.us.radio_news.fb<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample=='Facebook',],lm(I(radio_news/7)~1))
mean.us.radio_news.mt<-with(us.mt.anes[us.mt.anes$nontarg & us.mt.anes$sample=='MTurk',],lm(I(radio_news/7)~1))
mean.us.radio_news.qt<-with(us.qt.anes[us.qt.anes$nontarg & us.qt.anes$sample=='Qualtrics',],lm(I(radio_news/7)~1))

mean.us.radio_news.fb.wtd<-svyglm(I(radio_news/7)~1, design= us.fb.design)
mean.us.radio_news.mt.wtd<-svyglm(I(radio_news/7)~1, design= us.mt.design)
mean.us.radio_news.qt.wtd<-svyglm(I(radio_news/7)~1, design= us.qt.design)

sd.us.radio_news.anes<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample==' ANES',],sd(I(radio_news/7),na.rm=T))

# Paper news
mean.us.paper_news.anes<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample==' ANES',],lm(I(paper_news/7)~1))
mean.us.paper_news.fb<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample=='Facebook',],lm(I(paper_news/7)~1))
mean.us.paper_news.mt<-with(us.mt.anes[us.mt.anes$nontarg & us.mt.anes$sample=='MTurk',],lm(I(paper_news/7)~1))
mean.us.paper_news.qt<-with(us.qt.anes[us.qt.anes$nontarg & us.qt.anes$sample=='Qualtrics',],lm(I(paper_news/7)~1))

mean.us.paper_news.fb.wtd<-svyglm(I(paper_news/7)~1, design= us.fb.design)
mean.us.paper_news.mt.wtd<-svyglm(I(paper_news/7)~1, design= us.mt.design)
mean.us.paper_news.qt.wtd<-svyglm(I(paper_news/7)~1, design= us.qt.design)

sd.us.paper_news.anes<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample==' ANES',],sd(I(paper_news/7),na.rm=T))

# Know presidential term limit
mean.us.know_reelec.anes<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample==' ANES',],lm(Q13_correct~1))
mean.us.know_reelec.fb<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample=='Facebook',],lm(Q13_correct~1))
mean.us.know_reelec.mt<-with(us.mt.anes[us.mt.anes$nontarg & us.mt.anes$sample=='MTurk',],lm(Q13_correct~1))
mean.us.know_reelec.qt<-with(us.qt.anes[us.qt.anes$nontarg & us.qt.anes$sample=='Qualtrics',],lm(Q13_correct~1))

mean.us.know_reelec.fb.wtd<-svyglm(Q13_correct~1, design= us.fb.design)
mean.us.know_reelec.mt.wtd<-svyglm(Q13_correct~1, design= us.mt.design)
mean.us.know_reelec.qt.wtd<-svyglm(Q13_correct~1, design= us.qt.design)

sd.us.know_reelec.anes<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample==' ANES',],sd(Q13_correct,na.rm=T))

# Know length of a senate term
mean.us.know_senate.anes<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample==' ANES',],lm(Q14_correct~1))
mean.us.know_senate.fb<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample=='Facebook',],lm(Q14_correct~1))
mean.us.know_senate.mt<-with(us.mt.anes[us.mt.anes$nontarg & us.mt.anes$sample=='MTurk',],lm(Q14_correct~1))
mean.us.know_senate.qt<-with(us.qt.anes[us.qt.anes$nontarg & us.qt.anes$sample=='Qualtrics',],lm(Q14_correct~1))

mean.us.know_senate.fb.wtd<-svyglm(Q14_correct~1, design= us.fb.design)
mean.us.know_senate.mt.wtd<-svyglm(Q14_correct~1, design= us.mt.design)
mean.us.know_senate.qt.wtd<-svyglm(Q14_correct~1, design= us.qt.design)

sd.us.know_senate.anes<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample==' ANES',],sd(Q14_correct,na.rm=T))

# Know David Cameron office
mean.us.know_cameron.anes<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample==' ANES',],lm(Q15_correct~1))
mean.us.know_cameron.fb<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample=='Facebook',],lm(Q15_correct~1))
mean.us.know_cameron.mt<-with(us.mt.anes[us.mt.anes$nontarg & us.mt.anes$sample=='MTurk',],lm(Q15_correct~1))
mean.us.know_cameron.qt<-with(us.qt.anes[us.qt.anes$nontarg & us.qt.anes$sample=='Qualtrics',],lm(Q15_correct~1))

mean.us.know_cameron.fb.wtd<-svyglm(Q15_correct~1, design= us.fb.design)
mean.us.know_cameron.mt.wtd<-svyglm(Q15_correct~1, design= us.mt.design)
mean.us.know_cameron.qt.wtd<-svyglm(Q15_correct~1, design= us.qt.design)

sd.us.know_cameron.anes<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample==' ANES',],sd(Q15_correct,na.rm=T))

# Know John Roberts office
mean.us.know_roberts.anes<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample==' ANES',],lm(Q16_correct~1))
mean.us.know_roberts.fb<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample=='Facebook',],lm(Q16_correct~1))
mean.us.know_roberts.mt<-with(us.mt.anes[us.mt.anes$nontarg & us.mt.anes$sample=='MTurk',],lm(Q16_correct~1))
mean.us.know_roberts.qt<-with(us.qt.anes[us.qt.anes$nontarg & us.qt.anes$sample=='Qualtrics',],lm(Q16_correct~1))

mean.us.know_roberts.fb.wtd<-svyglm(Q16_correct~1, design= us.fb.design)
mean.us.know_roberts.mt.wtd<-svyglm(Q16_correct~1, design= us.mt.design)
mean.us.know_roberts.qt.wtd<-svyglm(Q16_correct~1, design= us.qt.design)

sd.us.know_roberts.anes<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample==' ANES',],sd(Q16_correct,na.rm=T))

# Know Medicare
mean.us.know_medicare.anes<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample==' ANES',],lm(Q17_correct~1))
mean.us.know_medicare.fb<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample=='Facebook',],lm(Q17_correct~1))
mean.us.know_medicare.mt<-with(us.mt.anes[us.mt.anes$nontarg & us.mt.anes$sample=='MTurk',],lm(Q17_correct~1))
mean.us.know_medicare.qt<-with(us.qt.anes[us.qt.anes$nontarg & us.qt.anes$sample=='Qualtrics',],lm(Q17_correct~1))

mean.us.know_medicare.fb.wtd<-svyglm(Q17_correct~1, design= us.fb.design)
mean.us.know_medicare.mt.wtd<-svyglm(Q17_correct~1, design= us.mt.design)
mean.us.know_medicare.qt.wtd<-svyglm(Q17_correct~1, design= us.qt.design)

sd.us.know_medicare.anes<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample==' ANES',],sd(Q17_correct,na.rm=T))

# Registered
mean.us.registered.anes<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample==' ANES',],lm(registered~1))
mean.us.registered.fb<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample=='Facebook',],lm(registered~1))
mean.us.registered.mt<-with(us.mt.anes[us.mt.anes$nontarg & us.mt.anes$sample=='MTurk',],lm(registered~1))
mean.us.registered.qt<-with(us.qt.anes[us.qt.anes$nontarg & us.qt.anes$sample=='Qualtrics',],lm(registered~1))

mean.us.registered.fb.wtd<-svyglm(registered~1, design= us.fb.design)
mean.us.registered.mt.wtd<-svyglm(registered~1, design= us.mt.design)
mean.us.registered.qt.wtd<-svyglm(registered~1, design= us.qt.design)

sd.us.registered.anes<-with(us.fb.anes[us.fb.anes$nontarg & us.fb.anes$sample==' ANES',],sd(registered,na.rm=T))

# Voted
mean.us.voted.gss<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample==' GSS',],lm(voted~1))
mean.us.voted.fb<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample=='Facebook',],lm(voted~1))
mean.us.voted.mt<-with(us.mt.gss[us.mt.gss$nontarg & us.mt.gss$sample=='MTurk',],lm(voted~1))
mean.us.voted.qt<-with(us.qt.gss[us.qt.gss$nontarg & us.qt.gss$sample=='Qualtrics',],lm(voted~1))

mean.us.voted.fb.wtd<-svyglm(voted~1, design= us.fb.design)
mean.us.voted.mt.wtd<-svyglm(voted~1, design= us.mt.design)
mean.us.voted.qt.wtd<-svyglm(voted~1, design= us.qt.design)

sd.us.voted.gss<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample==' GSS',],sd(voted,na.rm=T))

# Voted Obama
mean.us.voted_obama.gss<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample==' GSS',],lm(voted_obama~1))
mean.us.voted_obama.fb<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample=='Facebook',],lm(voted_obama~1))
mean.us.voted_obama.mt<-with(us.mt.gss[us.mt.gss$nontarg & us.mt.gss$sample=='MTurk',],lm(voted_obama~1))
mean.us.voted_obama.qt<-with(us.qt.gss[us.qt.gss$nontarg & us.qt.gss$sample=='Qualtrics',],lm(voted_obama~1))

mean.us.voted_obama.fb.wtd<-svyglm(voted_obama~1, design= us.fb.design)
mean.us.voted_obama.mt.wtd<-svyglm(voted_obama~1, design= us.mt.design)
mean.us.voted_obama.qt.wtd<-svyglm(voted_obama~1, design= us.qt.design)

sd.us.voted_obama.gss<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample==' GSS',],sd(voted_obama,na.rm=T))

# Voted Romney
mean.us.voted_romney.gss<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample==' GSS',],lm(voted_romney~1))
mean.us.voted_romney.fb<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample=='Facebook',],lm(voted_romney~1))
mean.us.voted_romney.mt<-with(us.mt.gss[us.mt.gss$nontarg & us.mt.gss$sample=='MTurk',],lm(voted_romney~1))
mean.us.voted_romney.qt<-with(us.qt.gss[us.qt.gss$nontarg & us.qt.gss$sample=='Qualtrics',],lm(voted_romney~1))

mean.us.voted_romney.fb.wtd<-svyglm(voted_romney~1, design= us.fb.design)
mean.us.voted_romney.mt.wtd<-svyglm(voted_romney~1, design= us.mt.design)
mean.us.voted_romney.qt.wtd<-svyglm(voted_romney~1, design= us.qt.design)

sd.us.voted_romney.gss<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample==' GSS',],sd(voted_romney,na.rm=T))

# Voted other
mean.us.voted_other.gss<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample==' GSS',],lm(voted_other~1))
mean.us.voted_other.fb<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample=='Facebook',],lm(voted_other~1))
mean.us.voted_other.mt<-with(us.mt.gss[us.mt.gss$nontarg & us.mt.gss$sample=='MTurk',],lm(voted_other~1))
mean.us.voted_other.qt<-with(us.qt.gss[us.qt.gss$nontarg & us.qt.gss$sample=='Qualtrics',],lm(voted_other~1))

mean.us.voted_other.fb.wtd<-svyglm(voted_other~1, design= us.fb.design)
mean.us.voted_other.mt.wtd<-svyglm(voted_other~1, design= us.mt.design)
mean.us.voted_other.qt.wtd<-svyglm(voted_other~1, design= us.qt.design)

sd.us.voted_other.gss<-with(us.fb.gss[us.fb.gss$nontarg & us.fb.gss$sample==' GSS',],sd(voted_other,na.rm=T))

# Gather coefficients

mean.us.pol1.rep<-sapply(list(mean.us.ideol.gss, mean.us.dem_rep.gss, mean.us.interest.gss, mean.us.registered.anes, mean.us.voted.gss, mean.us.voted_obama.gss, mean.us.voted_romney.gss, mean.us.voted_other.gss),coef)
mean.us.pol1.fb<-sapply(list(mean.us.ideol.fb, mean.us.dem_rep.fb, mean.us.interest.fb, mean.us.registered.fb, mean.us.voted.fb, mean.us.voted_obama.fb, mean.us.voted_romney.fb, mean.us.voted_other.fb),coef)
mean.us.pol1.mt<-sapply(list(mean.us.ideol.mt, mean.us.dem_rep.mt, mean.us.interest.mt, mean.us.registered.mt, mean.us.voted.mt, mean.us.voted_obama.mt, mean.us.voted_romney.mt, mean.us.voted_other.mt),coef)
mean.us.pol1.qt<-sapply(list(mean.us.ideol.qt, mean.us.dem_rep.qt, mean.us.interest.qt, mean.us.registered.qt, mean.us.voted.qt, mean.us.voted_obama.qt, mean.us.voted_romney.qt, mean.us.voted_other.qt),coef)

mean.us.pol2.rep<-sapply(list(mean.us.internet_news.anes, mean.us.tv_news.anes, mean.us.radio_news.anes, mean.us.paper_news.anes, mean.us.know_reelec.anes, mean.us.know_senate.anes, mean.us.know_cameron.anes, mean.us.know_roberts.anes, mean.us.know_medicare.anes),coef)
mean.us.pol2.fb<-sapply(list(mean.us.internet_news.fb, mean.us.tv_news.fb, mean.us.radio_news.fb, mean.us.paper_news.fb, mean.us.know_reelec.fb, mean.us.know_senate.fb, mean.us.know_cameron.fb, mean.us.know_roberts.fb, mean.us.know_medicare.fb),coef)
mean.us.pol2.mt<-sapply(list(mean.us.internet_news.mt, mean.us.tv_news.mt, mean.us.radio_news.mt, mean.us.paper_news.mt, mean.us.know_reelec.mt, mean.us.know_senate.mt, mean.us.know_cameron.mt, mean.us.know_roberts.mt, mean.us.know_medicare.mt),coef)
mean.us.pol2.qt<-sapply(list(mean.us.internet_news.qt, mean.us.tv_news.qt, mean.us.radio_news.qt, mean.us.paper_news.qt, mean.us.know_reelec.qt, mean.us.know_senate.qt, mean.us.know_cameron.qt, mean.us.know_roberts.qt, mean.us.know_medicare.qt),coef)

mean.us.pol1.fb.wtd<-sapply(list(mean.us.ideol.fb.wtd, mean.us.dem_rep.fb.wtd, mean.us.interest.fb.wtd, mean.us.registered.fb.wtd, mean.us.voted.fb.wtd, mean.us.voted_obama.fb.wtd, mean.us.voted_romney.fb.wtd, mean.us.voted_other.fb.wtd),coef)
mean.us.pol1.mt.wtd<-sapply(list(mean.us.ideol.mt.wtd, mean.us.dem_rep.mt.wtd, mean.us.interest.mt.wtd, mean.us.registered.mt.wtd, mean.us.voted.mt.wtd, mean.us.voted_obama.mt.wtd, mean.us.voted_romney.mt.wtd, mean.us.voted_other.mt.wtd),coef)
mean.us.pol1.qt.wtd<-sapply(list(mean.us.ideol.qt.wtd, mean.us.dem_rep.qt.wtd, mean.us.interest.qt.wtd, mean.us.registered.qt.wtd, mean.us.voted.qt.wtd, mean.us.voted_obama.qt.wtd, mean.us.voted_romney.qt.wtd, mean.us.voted_other.qt.wtd),coef)
mean.us.pol2.fb.wtd<-sapply(list(mean.us.internet_news.fb.wtd, mean.us.tv_news.fb.wtd, mean.us.radio_news.fb.wtd, mean.us.paper_news.fb.wtd, mean.us.know_reelec.fb.wtd, mean.us.know_senate.fb.wtd, mean.us.know_cameron.fb.wtd, mean.us.know_roberts.fb.wtd, mean.us.know_medicare.fb.wtd),coef)
mean.us.pol2.mt.wtd<-sapply(list(mean.us.internet_news.mt.wtd, mean.us.tv_news.mt.wtd, mean.us.radio_news.mt.wtd, mean.us.paper_news.mt.wtd, mean.us.know_reelec.mt.wtd, mean.us.know_senate.mt.wtd, mean.us.know_cameron.mt.wtd, mean.us.know_roberts.mt.wtd, mean.us.know_medicare.mt.wtd),coef)
mean.us.pol2.qt.wtd<-sapply(list(mean.us.internet_news.qt.wtd, mean.us.tv_news.qt.wtd, mean.us.radio_news.qt.wtd, mean.us.paper_news.qt.wtd, mean.us.know_reelec.qt.wtd, mean.us.know_senate.qt.wtd, mean.us.know_cameron.qt.wtd, mean.us.know_roberts.qt.wtd, mean.us.know_medicare.qt.wtd),coef)

se.us.pol1.rep<-sapply(list(mean.us.ideol.gss, mean.us.dem_rep.gss, mean.us.interest.gss, mean.us.registered.anes, mean.us.voted.gss, mean.us.voted_obama.gss, mean.us.voted_romney.gss, mean.us.voted_other.gss),function(x) coef(summary(x))[2])
se.us.pol1.fb<-sapply(list(mean.us.ideol.fb, mean.us.dem_rep.fb, mean.us.interest.fb, mean.us.registered.fb, mean.us.voted.fb, mean.us.voted_obama.fb, mean.us.voted_romney.fb, mean.us.voted_other.fb),function(x) coef(summary(x))[2])
se.us.pol1.mt<-sapply(list(mean.us.ideol.mt, mean.us.dem_rep.mt, mean.us.interest.mt, mean.us.registered.mt, mean.us.voted.mt, mean.us.voted_obama.mt, mean.us.voted_romney.mt, mean.us.voted_other.mt),function(x) coef(summary(x))[2])
se.us.pol1.qt<-sapply(list(mean.us.ideol.qt, mean.us.dem_rep.qt, mean.us.interest.qt, mean.us.registered.qt, mean.us.voted.qt, mean.us.voted_obama.qt, mean.us.voted_romney.qt, mean.us.voted_other.qt),function(x) coef(summary(x))[2])

se.us.pol2.rep<-sapply(list(mean.us.internet_news.anes, mean.us.tv_news.anes, mean.us.radio_news.anes, mean.us.paper_news.anes, mean.us.know_reelec.anes, mean.us.know_senate.anes, mean.us.know_cameron.anes, mean.us.know_roberts.anes, mean.us.know_medicare.anes),function(x) coef(summary(x))[2])
se.us.pol2.fb<-sapply(list(mean.us.internet_news.fb, mean.us.tv_news.fb, mean.us.radio_news.fb, mean.us.paper_news.fb, mean.us.know_reelec.fb, mean.us.know_senate.fb, mean.us.know_cameron.fb, mean.us.know_roberts.fb, mean.us.know_medicare.fb),function(x) coef(summary(x))[2])
se.us.pol2.mt<-sapply(list(mean.us.internet_news.mt, mean.us.tv_news.mt, mean.us.radio_news.mt, mean.us.paper_news.mt, mean.us.know_reelec.mt, mean.us.know_senate.mt, mean.us.know_cameron.mt, mean.us.know_roberts.mt, mean.us.know_medicare.mt),function(x) coef(summary(x))[2])
se.us.pol2.qt<-sapply(list(mean.us.internet_news.qt, mean.us.tv_news.qt, mean.us.radio_news.qt, mean.us.paper_news.qt, mean.us.know_reelec.qt, mean.us.know_senate.qt, mean.us.know_cameron.qt, mean.us.know_roberts.qt, mean.us.know_medicare.qt),function(x) coef(summary(x))[2])

se.us.pol1.fb.wtd<-sapply(list(mean.us.ideol.fb.wtd, mean.us.dem_rep.fb.wtd, mean.us.interest.fb.wtd, mean.us.registered.fb.wtd, mean.us.voted.fb.wtd, mean.us.voted_obama.fb.wtd, mean.us.voted_romney.fb.wtd, mean.us.voted_other.fb.wtd),function(x) coef(summary(x))[2])
se.us.pol1.mt.wtd<-sapply(list(mean.us.ideol.mt.wtd, mean.us.dem_rep.mt.wtd, mean.us.interest.mt.wtd, mean.us.registered.mt.wtd, mean.us.voted.mt.wtd, mean.us.voted_obama.mt.wtd, mean.us.voted_romney.mt.wtd, mean.us.voted_other.mt.wtd),function(x) coef(summary(x))[2])
se.us.pol1.qt.wtd<-sapply(list(mean.us.ideol.qt.wtd, mean.us.dem_rep.qt.wtd, mean.us.interest.qt.wtd, mean.us.registered.qt.wtd, mean.us.voted.qt.wtd, mean.us.voted_obama.qt.wtd, mean.us.voted_romney.qt.wtd, mean.us.voted_other.qt.wtd),function(x) coef(summary(x))[2])
se.us.pol2.fb.wtd<-sapply(list(mean.us.internet_news.fb.wtd, mean.us.tv_news.fb.wtd, mean.us.radio_news.fb.wtd, mean.us.paper_news.fb.wtd, mean.us.know_reelec.fb.wtd, mean.us.know_senate.fb.wtd, mean.us.know_cameron.fb.wtd, mean.us.know_roberts.fb.wtd, mean.us.know_medicare.fb.wtd),function(x) coef(summary(x))[2])
se.us.pol2.mt.wtd<-sapply(list(mean.us.internet_news.mt.wtd, mean.us.tv_news.mt.wtd, mean.us.radio_news.mt.wtd, mean.us.paper_news.mt.wtd, mean.us.know_reelec.mt.wtd, mean.us.know_senate.mt.wtd, mean.us.know_cameron.mt.wtd, mean.us.know_roberts.mt.wtd, mean.us.know_medicare.mt.wtd),function(x) coef(summary(x))[2])
se.us.pol2.qt.wtd<-sapply(list(mean.us.internet_news.qt.wtd, mean.us.tv_news.qt.wtd, mean.us.radio_news.qt.wtd, mean.us.paper_news.qt.wtd, mean.us.know_reelec.qt.wtd, mean.us.know_senate.qt.wtd, mean.us.know_cameron.qt.wtd, mean.us.know_roberts.qt.wtd, mean.us.know_medicare.qt.wtd),function(x) coef(summary(x))[2])

diff.us.pol1.fb<-abs(mean.us.pol1.fb-mean.us.pol1.rep)
diff.us.pol1.mt<-abs(mean.us.pol1.mt-mean.us.pol1.rep)
diff.us.pol1.qt<-abs(mean.us.pol1.qt-mean.us.pol1.rep)
which.closest<-apply(cbind(diff.us.pol1.fb,diff.us.pol1.mt,diff.us.pol1.qt),1, which.min)
col.us.pol1.fb<-ifelse(which.closest==1,'red','black')
col.us.pol1.mt<-ifelse(which.closest==2,'red','black')
col.us.pol1.qt<-ifelse(which.closest==3,'red','black')

diff.us.pol2.fb<-abs(mean.us.pol2.fb-mean.us.pol2.rep)
diff.us.pol2.mt<-abs(mean.us.pol2.mt-mean.us.pol2.rep)
diff.us.pol2.qt<-abs(mean.us.pol2.qt-mean.us.pol2.rep)
which.closest<-apply(cbind(diff.us.pol2.fb,diff.us.pol2.mt,diff.us.pol2.qt),1, which.min)
col.us.pol2.fb<-ifelse(which.closest==1,'red','black')
col.us.pol2.mt<-ifelse(which.closest==2,'red','black')
col.us.pol2.qt<-ifelse(which.closest==3,'red','black')

sd.us.pol.rep<-c(sd.us.ideol.gss, sd.us.dem_rep.gss, sd.us.interest.gss, sd.us.registered.anes, sd.us.voted.gss, sd.us.voted_obama.gss, sd.us.voted_romney.gss, sd.us.voted_other.gss, sd.us.internet_news.anes, sd.us.tv_news.anes, sd.us.radio_news.anes, sd.us.paper_news.anes, sd.us.know_reelec.anes, sd.us.know_senate.anes, sd.us.know_cameron.anes, sd.us.know_roberts.anes, sd.us.know_medicare.anes)

std.diff.pol.us.fb<-(c(mean.us.pol1.fb,mean.us.pol2.fb)-c(mean.us.pol1.rep,mean.us.pol2.rep))/sd.us.pol.rep
std.diff.pol.us.mt<-(c(mean.us.pol1.mt,mean.us.pol2.mt)-c(mean.us.pol1.rep,mean.us.pol2.rep))/sd.us.pol.rep
std.diff.pol.us.qt<-(c(mean.us.pol1.qt,mean.us.pol2.qt)-c(mean.us.pol1.rep,mean.us.pol2.rep))/sd.us.pol.rep
std.diff.pol.us.fb.wtd<-(c(mean.us.pol1.fb.wtd,mean.us.pol2.fb.wtd)-c(mean.us.pol1.rep,mean.us.pol2.rep))/sd.us.pol.rep
std.diff.pol.us.mt.wtd<-(c(mean.us.pol1.mt.wtd,mean.us.pol2.mt.wtd)-c(mean.us.pol1.rep,mean.us.pol2.rep))/sd.us.pol.rep
std.diff.pol.us.qt.wtd<-(c(mean.us.pol1.qt.wtd,mean.us.pol2.qt.wtd)-c(mean.us.pol1.rep,mean.us.pol2.rep))/sd.us.pol.rep

# Standardized difference table (Appendix Table 10)

us.stddiff.pol.table<-round(data.frame(fb= std.diff.pol.us.fb, fb.wtd= std.diff.pol.us.fb.wtd, mt= std.diff.pol.us.mt, mt.wtd= std.diff.pol.us.mt.wtd, qt= std.diff.pol.us.qt, qt.wtd= std.diff.pol.us.qt.wtd),2)
rownames(us.stddiff.pol.table)<-c('Lib.--Con.','Dem.--Repub.','Political interest','Registered to Vote','Turnout 2012','Obama','Romney','Other','Internet','Television','Radio','Newspaper','Pres. term limits','Senate term length','David Cameron','John Roberts','Medicare')
colnames(us.stddiff.pol.table)<-c('Facebook','Facebook\nWeighted','MTurk','MTurk\nWeighted','Qualtrics','Qualtrics\nWeighted')

us.stddiff.pol.table.note<-'NOTE: Entries are mean differences between the convenience and probability sample figures listed in Table \\ref{us.pol.table}, divided by the standard deviation in the probability sample.'

us.stddiff.pol.table.latex<-latex(us.stddiff.pol.table,file='us_stddiff_pol_table.tex', rowlabel = '', collabel.just=rep('c',ncol(us.stddiff.pol.table)), col.just=rep('c',ncol(us.stddiff.pol.table)), caption = 'U.S. Political Variables: Standardized Difference from Probability Samples', extracolsize='normalsize', insert.bottom= us.stddiff.pol.table.note, booktabs = F, ctable = T, where = "htp")

# Plot (Main Text Figure 4)

lwd<-1.5
adjust<-0.15
nvars<-length(mean.us.pol1.rep)

pdf(file='us_pol1.pdf',width=10,height=7)
par(mfrow=c(1,2))

par(mar=c(3,6,2,0)+.01)
plot(mean.us.pol1.rep, nvars:1+1.5*adjust, type='p',axes=F,frame.plot=T,xlab='',ylab='',pch=19,xlim=0:1,ylim=c(1-1.5*adjust,nvars+1.5*adjust),main='Unweighted',sub='')
Axis(side=1,at=seq(0,1,.25),labels= seq(0,1,.25))
Axis(side=2,at=c((nvars+1):0),labels=c('','Liberal-\nConservative','Democrat-\nRepublican','Political\nInterest', 'Registered\nto Vote', 'Turnout\n2012','Voted\nObama','Voted\nRomney','Voted\nOther',''),las=1)
abline(h=1:(nvars-1)+.5,lwd=1,lty=3)
segments(mean.us.pol1.rep-qnorm(.975)*se.us.pol1.rep,nvars:1+1.5*adjust, mean.us.pol1.rep+qnorm(.975)*se.us.pol1.rep,nvars:1+1.5*adjust,lwd=lwd)
segments(mean.us.pol1.fb-qnorm(.975)*se.us.pol1.fb,nvars:1+0.5*adjust, mean.us.pol1.fb+qnorm(.975)*se.us.pol1.fb,nvars:1+0.5*adjust,lwd=lwd)
segments(mean.us.pol1.mt-qnorm(.975)*se.us.pol1.mt,nvars:1-0.5*adjust, mean.us.pol1.mt+qnorm(.975)*se.us.pol1.mt,nvars:1-0.5*adjust,lwd=lwd)
segments(mean.us.pol1.qt-qnorm(.975)*se.us.pol1.qt,nvars:1-1.5*adjust, mean.us.pol1.qt+qnorm(.975)*se.us.pol1.qt,nvars:1-1.5*adjust,lwd=lwd)
points(mean.us.pol1.fb, nvars:1+0.5*adjust, pch=21, bg='white')
points(mean.us.pol1.mt, nvars:1-0.5*adjust, pch=8)
points(mean.us.pol1.qt, nvars:1-1.5*adjust, pch=25, bg='white')

par(mar=c(3,2,2,4)+.01)
plot(mean.us.pol1.rep, nvars:1+1.5*adjust, type='p',axes=F,frame.plot=T,xlab='',ylab='',pch=19,xlim=0:1,ylim=c(1-1.5*adjust,nvars+1.5*adjust),main='Weighted',sub='')
Axis(side=1,at=seq(0,1,.25),labels= seq(0,1,.25))
abline(h=1:(nvars-1)+.5,lwd=1,lty=3)
segments(mean.us.pol1.rep-qnorm(.975)*se.us.pol1.rep,nvars:1+1.5*adjust, mean.us.pol1.rep+qnorm(.975)*se.us.pol1.rep,nvars:1+1.5*adjust,lwd=lwd)
segments(mean.us.pol1.fb.wtd-qnorm(.975)*se.us.pol1.fb.wtd,nvars:1+0.5*adjust, mean.us.pol1.fb.wtd+qnorm(.975)*se.us.pol1.fb.wtd,nvars:1+0.5*adjust,lwd=lwd)
segments(mean.us.pol1.mt.wtd-qnorm(.975)*se.us.pol1.mt.wtd,nvars:1-0.5*adjust, mean.us.pol1.mt.wtd+qnorm(.975)*se.us.pol1.mt.wtd,nvars:1-0.5*adjust,lwd=lwd)
segments(mean.us.pol1.qt.wtd-qnorm(.975)*se.us.pol1.qt.wtd,nvars:1-1.5*adjust, mean.us.pol1.qt.wtd+qnorm(.975)*se.us.pol1.qt.wtd,nvars:1-1.5*adjust,lwd=lwd)
points(mean.us.pol1.fb.wtd, nvars:1+0.5*adjust, pch=21, bg='white')
points(mean.us.pol1.mt.wtd, nvars:1-0.5*adjust, pch=8)
points(mean.us.pol1.qt.wtd, nvars:1-1.5*adjust, pch=25, bg='white')
legend('bottomright',pch=c(19,21,8,25),legend=c('Probability','Facebook','MTurk','Qualtrics'),title='Sample',bg='white')

dev.off()

# Plot (Main Text Figure 5)

lwd<-1.5
adjust<-0.15
nvars<-length(mean.us.pol2.rep)

pdf(file='us_pol2.pdf', width=10,height=7)
par(mfrow=c(1,2))

par(mar=c(3,7,2,0)+.01)
plot(mean.us.pol2.rep, nvars:1+1.5*adjust, type='p',axes=F,frame.plot=T,xlab='',ylab='',pch=19,xlim=0:1,ylim=c(1-1.5*adjust,nvars+1.5*adjust),main='Unweighted',sub='')
Axis(side=1,at=seq(0,1,.25),labels= seq(0,1,.25))
Axis(side=2,at=c((nvars+1):0),labels=c('','Internet','Television','Radio', 'Newspaper', 'Presidential\nReelection','Senate\nTerm','David\nCameron','John\nRoberts','Medicare',''),las=1)
Axis(side=2,at=c(3,7.5),labels=c('Political Knowledge','News Exposure'), line=4.5,tick=F,font=2)
abline(h=1:(nvars-1)+.5,lwd=1,lty=3)
segments(mean.us.pol2.rep-qnorm(.975)*se.us.pol2.rep,nvars:1+1.5*adjust, mean.us.pol2.rep+qnorm(.975)*se.us.pol2.rep,nvars:1+1.5*adjust,lwd=lwd)
segments(mean.us.pol2.fb-qnorm(.975)*se.us.pol2.fb,nvars:1+0.5*adjust, mean.us.pol2.fb+qnorm(.975)*se.us.pol2.fb,nvars:1+0.5*adjust,lwd=lwd)
segments(mean.us.pol2.mt-qnorm(.975)*se.us.pol2.mt,nvars:1-0.5*adjust, mean.us.pol2.mt+qnorm(.975)*se.us.pol2.mt,nvars:1-0.5*adjust,lwd=lwd)
segments(mean.us.pol2.qt-qnorm(.975)*se.us.pol2.qt,nvars:1-1.5*adjust, mean.us.pol2.qt+qnorm(.975)*se.us.pol2.qt,nvars:1-1.5*adjust,lwd=lwd)
points(mean.us.pol2.fb, nvars:1+0.5*adjust, pch=21, bg='white')
points(mean.us.pol2.mt, nvars:1-0.5*adjust, pch=8)
points(mean.us.pol2.qt, nvars:1-1.5*adjust, pch=25, bg='white')

par(mar=c(3,2,2,5)+.01)
plot(mean.us.pol2.rep, nvars:1+1.5*adjust, type='p',axes=F,frame.plot=T,xlab='',ylab='',pch=19,xlim=0:1,ylim=c(1-1.5*adjust,nvars+1.5*adjust),main='Weighted',sub='')
Axis(side=1,at=seq(0,1,.25),labels= seq(0,1,.25))
abline(h=1:(nvars-1)+.5,lwd=1,lty=3)
segments(mean.us.pol2.rep-qnorm(.975)*se.us.pol2.rep,nvars:1+1.5*adjust, mean.us.pol2.rep+qnorm(.975)*se.us.pol2.rep,nvars:1+1.5*adjust,lwd=lwd)
segments(mean.us.pol2.fb.wtd-qnorm(.975)*se.us.pol2.fb.wtd,nvars:1+0.5*adjust, mean.us.pol2.fb.wtd+qnorm(.975)*se.us.pol2.fb.wtd,nvars:1+0.5*adjust,lwd=lwd)
segments(mean.us.pol2.mt.wtd-qnorm(.975)*se.us.pol2.mt.wtd,nvars:1-0.5*adjust, mean.us.pol2.mt.wtd+qnorm(.975)*se.us.pol2.mt.wtd,nvars:1-0.5*adjust,lwd=lwd)
segments(mean.us.pol2.qt.wtd-qnorm(.975)*se.us.pol2.qt.wtd,nvars:1-1.5*adjust, mean.us.pol2.qt.wtd+qnorm(.975)*se.us.pol2.qt.wtd,nvars:1-1.5*adjust,lwd=lwd)
points(mean.us.pol2.fb.wtd, nvars:1+0.5*adjust, pch=21, bg='white')
points(mean.us.pol2.mt.wtd, nvars:1-0.5*adjust, pch=8)
points(mean.us.pol2.qt.wtd, nvars:1-1.5*adjust, pch=25, bg='white')
legend('topleft',pch=c(19,21,8,25),legend=c('Probability','Facebook','MTurk','Qualtrics'),title='Sample',bg='white')

dev.off()

###################################
# Appendix Table 6: Indian Politics
###################################

# Generate stacked datasets and weighted design objects for each comparison

wvs$ideol<-unclass(wvs$V95)
india1$ideol<-india1$Q5

vote_bjp<-c(rep(1,6274),rep(0,(22301-6274-2099-444-55)),rep(NA, 2099+444+55))
vote_inc<-c(rep(1,3902),rep(0,(22301-3902-2099-444-55)),rep(NA, 2099+444+55))
vote_other<-c(rep(1,(22301-6274-3902-2099-444-55)),rep(0,(6274+3902)),rep(NA, 2099+444+55))
india1$vote_bjp<-as.numeric(india1$Q8==1)
india1$vote_inc<-as.numeric(india1$Q8==2)
india1$vote_other<-as.numeric(india1$Q8==3)

partyid<-c(rep(1,7068),rep(0,15232),NA)
india1$partyid<-as.numeric(india1$Q9==1)

# Reverse scale for news: more is higher
internet_news<- -1*(c(rep(1,1196),rep(2,1514),rep(3,1390),rep(4,15960),rep(NA,2241))) + 5
tv_news<- -1*(c(rep(1,10212),rep(2,5530),rep(3,2555),rep(4,3353),rep(NA,651))) + 5
radio_news<- -1*(c(rep(1,2027),rep(2,2726),rep(3,3285),rep(4,13110),rep(NA,1153))) + 5
paper_news<- -1*(c(rep(1,5605),rep(2,4194),rep(3,2506),rep(4,8914),rep(NA,1082))) + 5

india1$internet_news<- -1*(india1$Q12d) + 5
india1$tv_news<- -1*(india1$Q12a) + 5
india1$radio_news<- -1*(india1$Q12c) + 5
india1$paper_news<- -1*(india1$Q12b) + 5

# Not comparing knowledge of Prime Minister since SONS 2010 was only in a few states
know_cm<-c(rep(1,18117),rep(0,(22301-18117)))
know_republic<-c(rep(1,6731),rep(0,(15153-6731)))
know_gandhi<-c(rep(1,6026),rep(0,(15153-6026)))
india1$know_cm<-as.numeric(india1$Q14_correct)
india1$know_republic<-as.numeric(india1$Q15_correct)
india1$know_gandhi<-as.numeric(india1$Q16_correct)

registered<-c(rep(1,(33286+277)),rep(0,36641-33286-277-172),rep(NA,172))
india1$registered<-as.numeric(india1$Q6==1| india1$Q6==2)

voted<-c(rep(1,20202),rep(0,1964),rep(NA,135))
india1$voted<-as.numeric(india1$Q7==1)

wvs$interested<- -1*unclass(wvs$V84) + 5 # Reverse scale, more is higher
india1$interested<- -1*india1$Q4 + 5

india.fb.wvs<-rbind(india1[india1$sample=='Facebook',c('sample','weight','ideol','interested')],data.frame(sample=' WVS', weight=1, wvs[,c('ideol','interested')]))
india.fb.wvs.design<-svydesign(ids=~1,weights= india.fb.wvs$weight,data= india.fb.wvs)
india.mt.wvs<-rbind(india1[india1$sample=='MTurk',c('sample','weight','ideol','interested')],data.frame(sample=' WVS', weight=1, wvs[,c('ideol','interested')]))
india.mt.wvs.design<-svydesign(ids=~1,weights= india.mt.wvs$weight,data= india.mt.wvs)
india.qt.wvs<-rbind(india1[india1$sample=='Qualtrics',c('sample','weight','ideol','interested')],data.frame(sample=' WVS', weight=1, wvs[,c('ideol','interested')]))
india.qt.wvs.design<-svydesign(ids=~1,weights= india.qt.wvs$weight,data= india.qt.wvs)

india.fb.nes<-rbind(india1[india1$sample=='Facebook',c('sample','weight','vote_bjp','vote_inc','vote_other','voted','partyid','internet_news','tv_news','radio_news','paper_news','know_cm')],data.frame(sample=' NES', weight=1, vote_bjp, vote_inc, vote_other, voted, partyid, internet_news, tv_news, radio_news, paper_news, know_cm))
india.fb.nes.design<-svydesign(ids=~1,weights= india.fb.nes$weight,data= india.fb.nes)
india.mt.nes<-rbind(india1[india1$sample=='MTurk',c('sample','weight','vote_bjp','vote_inc','vote_other','voted','partyid','internet_news','tv_news','radio_news','paper_news','know_cm')],data.frame(sample=' NES', weight=1, vote_bjp, vote_inc, vote_other, voted, partyid, internet_news, tv_news, radio_news, paper_news, know_cm))
india.mt.nes.design<-svydesign(ids=~1,weights= india.mt.nes$weight,data= india.mt.nes)
india.qt.nes<-rbind(india1[india1$sample=='Qualtrics',c('sample','weight','vote_bjp','vote_inc','vote_other','voted','partyid','internet_news','tv_news','radio_news','paper_news','know_cm')],data.frame(sample=' NES', weight=1, vote_bjp, vote_inc, vote_other, voted, partyid, internet_news, tv_news, radio_news, paper_news, know_cm))
india.qt.nes.design<-svydesign(ids=~1,weights= india.qt.nes$weight,data= india.qt.nes)

india.fb.nes09<-rbind(india1[india1$sample=='Facebook',c('sample','weight','registered')],data.frame(sample=' NES09', weight=1, registered))
india.fb.nes09.design<-svydesign(ids=~1,weights= india.fb.nes09$weight,data= india.fb.nes09)
india.mt.nes09<-rbind(india1[india1$sample=='MTurk',c('sample','weight','registered')],data.frame(sample=' NES09', weight=1, registered))
india.mt.nes09.design<-svydesign(ids=~1,weights= india.mt.nes09$weight,data= india.mt.nes09)
india.qt.nes09<-rbind(india1[india1$sample=='Qualtrics',c('sample','weight','registered')],data.frame(sample=' NES09', weight=1, registered))
india.qt.nes09.design<-svydesign(ids=~1,weights= india.qt.nes09$weight,data= india.qt.nes09)

india.fb.sons<-rbind(india1[india1$sample=='Facebook',c('sample','weight','know_republic','know_gandhi')],data.frame(sample=' SONS', weight=1, know_republic, know_gandhi))
india.fb.sons.design<-svydesign(ids=~1,weights= india.fb.sons$weight,data= india.fb.sons)
india.mt.sons<-rbind(india1[india1$sample=='MTurk',c('sample','weight','know_republic','know_gandhi')],data.frame(sample=' SONS', weight=1, know_republic, know_gandhi))
india.mt.sons.design<-svydesign(ids=~1,weights= india.mt.sons$weight,data= india.mt.sons)
india.qt.sons<-rbind(india1[india1$sample=='Qualtrics',c('sample','weight','know_republic','know_gandhi')],data.frame(sample=' SONS', weight=1, know_republic, know_gandhi))
india.qt.sons.design<-svydesign(ids=~1,weights= india.qt.sons$weight,data= india.qt.sons)

# Ideology
reg.india.ideol.fb<-with(india.fb.wvs,lm(ideol~sample))
reg.india.ideol.fb.wtd<-svyglm(ideol~sample, design= india.fb.wvs.design)
reg.india.ideol.mt<-with(india.mt.wvs,lm(ideol~sample))
reg.india.ideol.mt.wtd<-svyglm(ideol~sample, design= india.mt.wvs.design)
reg.india.ideol.qt<-with(india.qt.wvs,lm(ideol~sample))
reg.india.ideol.qt.wtd<-svyglm(ideol~sample, design= india.qt.wvs.design)

# Voted
reg.india.voted.fb<-with(india.fb.nes,lm(voted~sample))
reg.india.voted.fb.wtd<-svyglm(voted~sample, design= india.fb.nes.design)
reg.india.voted.mt<-with(india.mt.nes,lm(voted~sample))
reg.india.voted.mt.wtd<-svyglm(voted~sample, design= india.mt.nes.design)
reg.india.voted.qt<-with(india.qt.nes,lm(voted~sample))
reg.india.voted.qt.wtd<-svyglm(voted~sample, design= india.qt.nes.design)

# Voted BJP
reg.india.vote_bjp.fb<-with(india.fb.nes,lm(vote_bjp~sample))
reg.india.vote_bjp.fb.wtd<-svyglm(vote_bjp~sample, design= india.fb.nes.design)
reg.india.vote_bjp.mt<-with(india.mt.nes,lm(vote_bjp~sample))
reg.india.vote_bjp.mt.wtd<-svyglm(vote_bjp~sample, design= india.mt.nes.design)
reg.india.vote_bjp.qt<-with(india.qt.nes,lm(vote_bjp~sample))
reg.india.vote_bjp.qt.wtd<-svyglm(vote_bjp~sample, design= india.qt.nes.design)

# Voted INC
reg.india.vote_inc.fb<-with(india.fb.nes,lm(vote_inc~sample))
reg.india.vote_inc.fb.wtd<-svyglm(vote_inc~sample, design= india.fb.nes.design)
reg.india.vote_inc.mt<-with(india.mt.nes,lm(vote_inc~sample))
reg.india.vote_inc.mt.wtd<-svyglm(vote_inc~sample, design= india.mt.nes.design)
reg.india.vote_inc.qt<-with(india.qt.nes,lm(vote_inc~sample))
reg.india.vote_inc.qt.wtd<-svyglm(vote_inc~sample, design= india.qt.nes.design)

# Voted other
reg.india.vote_other.fb<-with(india.fb.nes,lm(vote_other~sample))
reg.india.vote_other.fb.wtd<-svyglm(vote_other~sample, design= india.fb.nes.design)
reg.india.vote_other.mt<-with(india.mt.nes,lm(vote_other~sample))
reg.india.vote_other.mt.wtd<-svyglm(vote_other~sample, design= india.mt.nes.design)
reg.india.vote_other.qt<-with(india.qt.nes,lm(vote_other~sample))
reg.india.vote_other.qt.wtd<-svyglm(vote_other~sample, design= india.qt.nes.design)

# Identify with any party
reg.india.partyid.fb<-with(india.fb.nes,lm(partyid~sample))
reg.india.partyid.fb.wtd<-svyglm(partyid~sample, design= india.fb.nes.design)
reg.india.partyid.mt<-with(india.mt.nes,lm(partyid~sample))
reg.india.partyid.mt.wtd<-svyglm(partyid~sample, design= india.mt.nes.design)
reg.india.partyid.qt<-with(india.qt.nes,lm(partyid~sample))
reg.india.partyid.qt.wtd<-svyglm(partyid~sample, design= india.qt.nes.design)

# Registered
reg.india.registered.fb<-with(india.fb.nes09,lm(registered~sample))
reg.india.registered.fb.wtd<-svyglm(registered~sample, design= india.fb.nes09.design)
reg.india.registered.mt<-with(india.mt.nes09,lm(registered~sample))
reg.india.registered.mt.wtd<-svyglm(registered~sample, design= india.mt.nes09.design)
reg.india.registered.qt<-with(india.qt.nes09,lm(registered~sample))
reg.india.registered.qt.wtd<-svyglm(registered~sample, design= india.qt.nes09.design)

# Interested
reg.india.interested.fb<-with(india.fb.wvs,lm(interested~sample))
reg.india.interested.fb.wtd<-svyglm(interested~sample, design= india.fb.wvs.design)
reg.india.interested.mt<-with(india.mt.wvs,lm(interested~sample))
reg.india.interested.mt.wtd<-svyglm(interested~sample, design= india.mt.wvs.design)
reg.india.interested.qt<-with(india.qt.wvs,lm(interested~sample))
reg.india.interested.qt.wtd<-svyglm(interested~sample, design= india.qt.wvs.design)

# Internet news
reg.india.internet_news.fb<-with(india.fb.nes,lm(internet_news~sample))
reg.india.internet_news.fb.wtd<-svyglm(internet_news~sample, design= india.fb.nes.design)
reg.india.internet_news.mt<-with(india.mt.nes,lm(internet_news~sample))
reg.india.internet_news.mt.wtd<-svyglm(internet_news~sample, design= india.mt.nes.design)
reg.india.internet_news.qt<-with(india.qt.nes,lm(internet_news~sample))
reg.india.internet_news.qt.wtd<-svyglm(internet_news~sample, design= india.qt.nes.design)

# TV news
reg.india.tv_news.fb<-with(india.fb.nes,lm(tv_news~sample))
reg.india.tv_news.fb.wtd<-svyglm(tv_news~sample, design= india.fb.nes.design)
reg.india.tv_news.mt<-with(india.mt.nes,lm(tv_news~sample))
reg.india.tv_news.mt.wtd<-svyglm(tv_news~sample, design= india.mt.nes.design)
reg.india.tv_news.qt<-with(india.qt.nes,lm(tv_news~sample))
reg.india.tv_news.qt.wtd<-svyglm(tv_news~sample, design= india.qt.nes.design)

# Radio news
reg.india.radio_news.fb<-with(india.fb.nes,lm(radio_news~sample))
reg.india.radio_news.fb.wtd<-svyglm(radio_news~sample, design= india.fb.nes.design)
reg.india.radio_news.mt<-with(india.mt.nes,lm(radio_news~sample))
reg.india.radio_news.mt.wtd<-svyglm(radio_news~sample, design= india.mt.nes.design)
reg.india.radio_news.qt<-with(india.qt.nes,lm(radio_news~sample))
reg.india.radio_news.qt.wtd<-svyglm(radio_news~sample, design= india.qt.nes.design)

# Newspaper news
reg.india.paper_news.fb<-with(india.fb.nes,lm(paper_news~sample))
reg.india.paper_news.fb.wtd<-svyglm(paper_news~sample, design= india.fb.nes.design)
reg.india.paper_news.mt<-with(india.mt.nes,lm(paper_news~sample))
reg.india.paper_news.mt.wtd<-svyglm(paper_news~sample, design= india.mt.nes.design)
reg.india.paper_news.qt<-with(india.qt.nes,lm(paper_news~sample))
reg.india.paper_news.qt.wtd<-svyglm(paper_news~sample, design= india.qt.nes.design)

# Know chief minister
reg.india.know_cm.fb<-with(india.fb.nes,lm(know_cm~sample))
reg.india.know_cm.fb.wtd<-svyglm(know_cm~sample, design= india.fb.nes.design)
reg.india.know_cm.mt<-with(india.mt.nes,lm(know_cm~sample))
reg.india.know_cm.mt.wtd<-svyglm(know_cm~sample, design= india.mt.nes.design)
reg.india.know_cm.qt<-with(india.qt.nes,lm(know_cm~sample))
reg.india.know_cm.qt.wtd<-svyglm(know_cm~sample, design= india.qt.nes.design)

# Know Republic Day
reg.india.know_republic.fb<-with(india.fb.sons,lm(know_republic~sample))
reg.india.know_republic.fb.wtd<-svyglm(know_republic~sample, design= india.fb.sons.design)
reg.india.know_republic.mt<-with(india.mt.sons,lm(know_republic~sample))
reg.india.know_republic.mt.wtd<-svyglm(know_republic~sample, design= india.mt.sons.design)
reg.india.know_republic.qt<-with(india.qt.sons,lm(know_republic~sample))
reg.india.know_republic.qt.wtd<-svyglm(know_republic~sample, design= india.qt.sons.design)

# Know Gandhi's birthday
reg.india.know_gandhi.fb<-with(india.fb.sons,lm(know_gandhi~sample))
reg.india.know_gandhi.fb.wtd<-svyglm(know_gandhi~sample, design= india.fb.sons.design)
reg.india.know_gandhi.mt<-with(india.mt.sons,lm(know_gandhi~sample))
reg.india.know_gandhi.mt.wtd<-svyglm(know_gandhi~sample, design= india.mt.sons.design)
reg.india.know_gandhi.qt<-with(india.qt.sons,lm(know_gandhi~sample))
reg.india.know_gandhi.qt.wtd<-svyglm(know_gandhi~sample, design= india.qt.sons.design)

# Gather coefficients for table

est.india.prob<-round(sapply(list(reg.india.ideol.fb, reg.india.partyid.fb, reg.india.interested.fb, reg.india.registered.fb, reg.india.voted.fb, reg.india.vote_bjp.fb, reg.india.vote_inc.fb, reg.india.vote_other.fb, reg.india.internet_news.fb, reg.india.tv_news.fb, reg.india.radio_news.fb, reg.india.paper_news.fb, reg.india.know_cm.fb, reg.india.know_republic.fb, reg.india.know_gandhi.fb), function(x) coef(x)[1]),2)
est.india.fb<-round(sapply(list(reg.india.ideol.fb, reg.india.partyid.fb, reg.india.interested.fb, reg.india.registered.fb, reg.india.voted.fb, reg.india.vote_bjp.fb, reg.india.vote_inc.fb, reg.india.vote_other.fb, reg.india.internet_news.fb, reg.india.tv_news.fb, reg.india.radio_news.fb, reg.india.paper_news.fb, reg.india.know_cm.fb, reg.india.know_republic.fb, reg.india.know_gandhi.fb), function(x) sum(coef(x))),2)
pval.india.fb<-sapply(list(reg.india.ideol.fb, reg.india.partyid.fb, reg.india.interested.fb, reg.india.registered.fb, reg.india.voted.fb, reg.india.vote_bjp.fb, reg.india.vote_inc.fb, reg.india.vote_other.fb, reg.india.internet_news.fb, reg.india.tv_news.fb, reg.india.radio_news.fb, reg.india.paper_news.fb, reg.india.know_cm.fb, reg.india.know_republic.fb, reg.india.know_gandhi.fb), pval)
est.india.fb.wtd<-round(sapply(list(reg.india.ideol.fb.wtd, reg.india.partyid.fb.wtd, reg.india.interested.fb.wtd, reg.india.registered.fb.wtd, reg.india.voted.fb.wtd, reg.india.vote_bjp.fb.wtd, reg.india.vote_inc.fb.wtd, reg.india.vote_other.fb.wtd, reg.india.internet_news.fb.wtd, reg.india.tv_news.fb.wtd, reg.india.radio_news.fb.wtd, reg.india.paper_news.fb.wtd, reg.india.know_cm.fb.wtd, reg.india.know_republic.fb.wtd, reg.india.know_gandhi.fb.wtd), function(x) sum(coef(x))),2)
pval.india.fb.wtd<-sapply(list(reg.india.ideol.fb.wtd, reg.india.partyid.fb.wtd, reg.india.interested.fb.wtd, reg.india.registered.fb.wtd, reg.india.voted.fb.wtd, reg.india.vote_bjp.fb.wtd, reg.india.vote_inc.fb.wtd, reg.india.vote_other.fb.wtd, reg.india.internet_news.fb.wtd, reg.india.tv_news.fb.wtd, reg.india.radio_news.fb.wtd, reg.india.paper_news.fb.wtd, reg.india.know_cm.fb.wtd, reg.india.know_republic.fb.wtd, reg.india.know_gandhi.fb.wtd), pval)
est.india.mt<-round(sapply(list(reg.india.ideol.mt, reg.india.partyid.mt, reg.india.interested.mt, reg.india.registered.mt, reg.india.voted.mt, reg.india.vote_bjp.mt, reg.india.vote_inc.mt, reg.india.vote_other.mt, reg.india.internet_news.mt, reg.india.tv_news.mt, reg.india.radio_news.mt, reg.india.paper_news.mt, reg.india.know_cm.mt, reg.india.know_republic.mt, reg.india.know_gandhi.mt), function(x) sum(coef(x))),2)
pval.india.mt<-sapply(list(reg.india.ideol.mt, reg.india.partyid.mt, reg.india.interested.mt, reg.india.registered.mt, reg.india.voted.mt, reg.india.vote_bjp.mt, reg.india.vote_inc.mt, reg.india.vote_other.mt, reg.india.internet_news.mt, reg.india.tv_news.mt, reg.india.radio_news.mt, reg.india.paper_news.mt, reg.india.know_cm.mt, reg.india.know_republic.mt, reg.india.know_gandhi.mt), pval)
est.india.mt.wtd<-round(sapply(list(reg.india.ideol.mt.wtd, reg.india.partyid.mt.wtd, reg.india.interested.mt.wtd, reg.india.registered.mt.wtd, reg.india.voted.mt.wtd, reg.india.vote_bjp.mt.wtd, reg.india.vote_inc.mt.wtd, reg.india.vote_other.mt.wtd, reg.india.internet_news.mt.wtd, reg.india.tv_news.mt.wtd, reg.india.radio_news.mt.wtd, reg.india.paper_news.mt.wtd, reg.india.know_cm.mt.wtd, reg.india.know_republic.mt.wtd, reg.india.know_gandhi.mt.wtd), function(x) sum(coef(x))),2)
pval.india.mt.wtd<-sapply(list(reg.india.ideol.mt.wtd, reg.india.partyid.mt.wtd, reg.india.interested.mt.wtd, reg.india.registered.mt.wtd, reg.india.voted.mt.wtd, reg.india.vote_bjp.mt.wtd, reg.india.vote_inc.mt.wtd, reg.india.vote_other.mt.wtd, reg.india.internet_news.mt.wtd, reg.india.tv_news.mt.wtd, reg.india.radio_news.mt.wtd, reg.india.paper_news.mt.wtd, reg.india.know_cm.mt.wtd, reg.india.know_republic.mt.wtd, reg.india.know_gandhi.mt.wtd), pval)
est.india.qt<-round(sapply(list(reg.india.ideol.qt, reg.india.partyid.qt, reg.india.interested.qt, reg.india.registered.qt, reg.india.voted.qt, reg.india.vote_bjp.qt, reg.india.vote_inc.qt, reg.india.vote_other.qt, reg.india.internet_news.qt, reg.india.tv_news.qt, reg.india.radio_news.qt, reg.india.paper_news.qt, reg.india.know_cm.qt, reg.india.know_republic.qt, reg.india.know_gandhi.qt), function(x) sum(coef(x))),2)
pval.india.qt<-sapply(list(reg.india.ideol.qt, reg.india.partyid.qt, reg.india.interested.qt, reg.india.registered.qt, reg.india.voted.qt, reg.india.vote_bjp.qt, reg.india.vote_inc.qt, reg.india.vote_other.qt, reg.india.internet_news.qt, reg.india.tv_news.qt, reg.india.radio_news.qt, reg.india.paper_news.qt, reg.india.know_cm.qt, reg.india.know_republic.qt, reg.india.know_gandhi.qt), pval)
est.india.qt.wtd<-round(sapply(list(reg.india.ideol.qt.wtd, reg.india.partyid.qt.wtd, reg.india.interested.qt.wtd, reg.india.registered.qt.wtd, reg.india.voted.qt.wtd, reg.india.vote_bjp.qt.wtd, reg.india.vote_inc.qt.wtd, reg.india.vote_other.qt.wtd, reg.india.internet_news.qt.wtd, reg.india.tv_news.qt.wtd, reg.india.radio_news.qt.wtd, reg.india.paper_news.qt.wtd, reg.india.know_cm.qt.wtd, reg.india.know_republic.qt.wtd, reg.india.know_gandhi.qt.wtd), function(x) sum(coef(x))),2)
pval.india.qt.wtd<-sapply(list(reg.india.ideol.qt.wtd, reg.india.partyid.qt.wtd, reg.india.interested.qt.wtd, reg.india.registered.qt.wtd, reg.india.voted.qt.wtd, reg.india.vote_bjp.qt.wtd, reg.india.vote_inc.qt.wtd, reg.india.vote_other.qt.wtd, reg.india.internet_news.qt.wtd, reg.india.tv_news.qt.wtd, reg.india.radio_news.qt.wtd, reg.india.paper_news.qt.wtd, reg.india.know_cm.qt.wtd, reg.india.know_republic.qt.wtd, reg.india.know_gandhi.qt.wtd), pval)

pval.adj.india<-p.adjust(c(pval.india.fb, pval.india.fb.wtd, pval.india.mt, pval.india.mt.wtd, pval.india.qt, pval.india.qt.wtd),method='BH')
stars.india<-data.frame(matrix(stars(pval.adj.india),nrow=length(est.india.prob)))
names(stars.india)<-c('fb','fb.wtd','mt','mt.wtd','qt','qt.wtd')

india.pol.table<-data.frame(prob=as.character(est.india.prob),fb=paste(est.india.fb,stars.india$fb,sep=''), fb.wtd=paste(est.india.fb.wtd,stars.india$fb.wtd,sep=''), mt=paste(est.india.mt,stars.india$mt,sep=''), mt.wtd=paste(est.india.mt.wtd,stars.india$mt.wtd,sep=''), qt=paste(est.india.qt,stars.india$qt,sep=''), qt.wtd=paste(est.india.qt.wtd,stars.india$qt.wtd,sep=''),stringsAsFactors=F)
rownames(india.pol.table)<-c('Left--Right (1--10)','Any party ID','Political interest (1--4)','Registered to Vote','Turnout 2014','BJP','INC','Other','Internet','Television','Radio','Newspaper','State Chief Minister','Republic Day',"Gandhi's Birthday")
colnames(india.pol.table)<-c('Probability\nSample','Facebook','Facebook\nWeighted','MTurk','MTurk\nWeighted','Qualtrics','Qualtrics\nWeighted')

# Bold results that are consistent with hypotheses; underline results that go against.
india.hypo.table<-matrix(
	c(NA,T,NA,T,NA,NA,NA,
	NA,T,NA,T,NA,NA,NA,
	NA,NA,NA,NA,NA,NA,NA,
	NA,NA,NA,NA,NA,NA,NA,
	NA,NA,NA,NA,NA,NA,NA,
	NA,T,NA,T,NA,NA,NA,
	NA,NA,NA,NA,NA,NA,NA,
	NA,NA,NA,NA,NA,NA,NA,
	NA,T,NA,T,NA,NA,NA,
	NA,T,NA,T,NA,NA,NA,
	NA,T,NA,T,NA,NA,NA,
	NA,T,NA,T,NA,NA,NA,
	NA,T,NA,T,NA,NA,NA,
	NA,T,NA,T,NA,NA,NA,
	NA,T,NA,T,NA,NA,NA), byrow=T,nrow=length(est.india.prob))

for (i in 1:nrow(india.pol.table)){
	for (j in 1:ncol(india.pol.table)){
		if (is.na(india.hypo.table[i,j])) next
		india.pol.table[i,j]<-ifelse(india.hypo.table[i,j], paste0('\\textbf{', india.pol.table[i,j],'}'), paste0('\\underline{', india.pol.table[i,j],'}'))
	}
}

india.pol.table.note<-"NOTE: Entries are mean or weighted mean values for each sample. Probability sample is the January 2006 State of the Nation Survey (knowledge of Republic Day and Gandhi's Birthday), the 2009 Indian National Election Study (voter registration), the 2014 World Values Survey (ideology and interest), or the 2014 Indian National Election Study, post-poll (all other variables). Weights are post-stratification, based on region (4 categories), age (above/below median), and sex in the 2011 census. Stars report significance levels from two-tailed difference-in-means t-tests comparing the probability and convenience sample quantities, with adjustment for multiple comparisons using the Benjamini-Hochberg method (all hypotheses in the table are a single family). Results in bold type support pre-registered hypotheses, underlined results contradict pre-registered hypotheses, and those in regular font were not pre-registered. $\\dagger$ p$<$0.1, *p$<$0.05, **p$<$0.01, ***p$<$0.001."

india.pol.table.latex<-latex(india.pol.table,file='india_pol_table.tex', rowlabel = '', collabel.just=rep('c',ncol(india.pol.table)), col.just=rep('c',ncol(india.pol.table)), rgroup=c('General','2014 Lower House Vote','News Exposure (1--4)', 'Political Knowledge'), n.rgroup=c(5,3,4,3), caption = 'India Political Variables: Convenience versus Probability Samples', extracolsize='normalsize', insert.bottom= india.pol.table.note, booktabs = F, ctable = T, where = "htp")

##################################################################
# Main Text Figures 6-7 and Appendix Table 11: Indian Demographics
##################################################################

# Weighted design objects, convenience samples only

india.fb.design<-svydesign(ids=~1,weights= india1$weight[india1$sample=='Facebook'],data= india1[india1$sample=='Facebook',])
india.mt.design<-svydesign(ids=~1,weights= india1$weight[india1$sample=='MTurk'],data= india1[india1$sample=='MTurk',])
india.qt.design<-svydesign(ids=~1,weights= india1$weight[india1$sample=='Qualtrics'],data= india1[india1$sample=='Qualtrics',])

# Ideology
mean.india.ideol.wvs<-with(india.fb.wvs[india.fb.wvs$sample==' WVS',],lm(I((ideol-1)/9)~1))
mean.india.ideol.fb<-with(india.fb.wvs[india.fb.wvs$sample=='Facebook',],lm(I((ideol-1)/9)~1))
mean.india.ideol.mt<-with(india.mt.wvs[india.mt.wvs$sample=='MTurk',],lm(I((ideol-1)/9)~1))
mean.india.ideol.qt<-with(india.qt.wvs[india.qt.wvs$sample=='Qualtrics',],lm(I((ideol-1)/9)~1))

mean.india.ideol.fb.wtd<-svyglm(I((ideol-1)/9)~1, design= india.fb.design)
mean.india.ideol.mt.wtd<-svyglm(I((ideol-1)/9)~1, design= india.mt.design)
mean.india.ideol.qt.wtd<-svyglm(I((ideol-1)/9)~1, design= india.qt.design)

sd.india.ideol.wvs<-with(india.fb.wvs[india.fb.wvs$sample==' WVS',],sd(I((ideol-1)/9),na.rm=T))

# Voted
mean.india.voted.nes<-with(india.fb.nes[india.fb.nes$sample==' NES',],lm(voted~1))
mean.india.voted.fb<-with(india.fb.nes[india.fb.nes$sample=='Facebook',],lm(voted~1))
mean.india.voted.mt<-with(india.mt.nes[india.mt.nes$sample=='MTurk',],lm(voted~1))
mean.india.voted.qt<-with(india.qt.nes[india.qt.nes$sample=='Qualtrics',],lm(voted~1))

mean.india.voted.fb.wtd<-svyglm(voted ~1, design= india.fb.design)
mean.india.voted.mt.wtd<-svyglm(voted ~1, design= india.mt.design)
mean.india.voted.qt.wtd<-svyglm(voted ~1, design= india.qt.design)

sd.india.voted.nes<-with(india.fb.nes[india.fb.nes$sample==' NES',],sd(voted,na.rm=T))

# Voted BJP
mean.india.vote_bjp.nes<-with(india.fb.nes[india.fb.nes$sample==' NES',],lm(vote_bjp~1))
mean.india.vote_bjp.fb<-with(india.fb.nes[india.fb.nes$sample=='Facebook',],lm(vote_bjp~1))
mean.india.vote_bjp.mt<-with(india.mt.nes[india.mt.nes$sample=='MTurk',],lm(vote_bjp~1))
mean.india.vote_bjp.qt<-with(india.qt.nes[india.qt.nes$sample=='Qualtrics',],lm(vote_bjp~1))

mean.india.vote_bjp.fb.wtd<-svyglm(vote_bjp ~1, design= india.fb.design)
mean.india.vote_bjp.mt.wtd<-svyglm(vote_bjp ~1, design= india.mt.design)
mean.india.vote_bjp.qt.wtd<-svyglm(vote_bjp ~1, design= india.qt.design)

sd.india.vote_bjp.nes<-with(india.fb.nes[india.fb.nes$sample==' NES',],sd(vote_bjp,na.rm=T))

# Voted INC
mean.india.vote_inc.nes<-with(india.fb.nes[india.fb.nes$sample==' NES',],lm(vote_inc~1))
mean.india.vote_inc.fb<-with(india.fb.nes[india.fb.nes$sample=='Facebook',],lm(vote_inc~1))
mean.india.vote_inc.mt<-with(india.mt.nes[india.mt.nes$sample=='MTurk',],lm(vote_inc~1))
mean.india.vote_inc.qt<-with(india.qt.nes[india.qt.nes$sample=='Qualtrics',],lm(vote_inc~1))

mean.india.vote_inc.fb.wtd<-svyglm(vote_inc ~1, design= india.fb.design)
mean.india.vote_inc.mt.wtd<-svyglm(vote_inc ~1, design= india.mt.design)
mean.india.vote_inc.qt.wtd<-svyglm(vote_inc ~1, design= india.qt.design)

sd.india.vote_inc.nes<-with(india.fb.nes[india.fb.nes$sample==' NES',],sd(vote_inc,na.rm=T))

# Voted other
mean.india.vote_other.nes<-with(india.fb.nes[india.fb.nes$sample==' NES',],lm(vote_other~1))
mean.india.vote_other.fb<-with(india.fb.nes[india.fb.nes$sample=='Facebook',],lm(vote_other~1))
mean.india.vote_other.mt<-with(india.mt.nes[india.mt.nes$sample=='MTurk',],lm(vote_other~1))
mean.india.vote_other.qt<-with(india.qt.nes[india.qt.nes$sample=='Qualtrics',],lm(vote_other~1))

mean.india.vote_other.fb.wtd<-svyglm(vote_other ~1, design= india.fb.design)
mean.india.vote_other.mt.wtd<-svyglm(vote_other ~1, design= india.mt.design)
mean.india.vote_other.qt.wtd<-svyglm(vote_other ~1, design= india.qt.design)

sd.india.vote_other.nes<-with(india.fb.nes[india.fb.nes$sample==' NES',],sd(vote_other,na.rm=T))

# Identify with any party
mean.india.partyid.nes<-with(india.fb.nes[india.fb.nes$sample==' NES',],lm(partyid~1))
mean.india.partyid.fb<-with(india.fb.nes[india.fb.nes$sample=='Facebook',],lm(partyid~1))
mean.india.partyid.mt<-with(india.mt.nes[india.mt.nes$sample=='MTurk',],lm(partyid~1))
mean.india.partyid.qt<-with(india.qt.nes[india.qt.nes$sample=='Qualtrics',],lm(partyid~1))

mean.india.partyid.fb.wtd<-svyglm(partyid ~1, design= india.fb.design)
mean.india.partyid.mt.wtd<-svyglm(partyid ~1, design= india.mt.design)
mean.india.partyid.qt.wtd<-svyglm(partyid ~1, design= india.qt.design)

sd.india.partyid.nes<-with(india.fb.nes[india.fb.nes$sample==' NES',],sd(partyid,na.rm=T))

# Registered
mean.india.registered.nes09<-with(india.fb.nes09[india.fb.nes09$sample==' NES09',],lm(registered~1))
mean.india.registered.fb<-with(india.fb.nes09[india.fb.nes09$sample=='Facebook',],lm(registered~1))
mean.india.registered.mt<-with(india.mt.nes09[india.mt.nes09$sample=='MTurk',],lm(registered~1))
mean.india.registered.qt<-with(india.qt.nes09[india.qt.nes09$sample=='Qualtrics',],lm(registered~1))

mean.india.registered.fb.wtd<-svyglm(registered ~1, design= india.fb.design)
mean.india.registered.mt.wtd<-svyglm(registered ~1, design= india.mt.design)
mean.india.registered.qt.wtd<-svyglm(registered ~1, design= india.qt.design)

sd.india.registered.nes09<-with(india.fb.nes09[india.fb.nes09$sample==' NES09',],sd(registered,na.rm=T))

# Interested
mean.india.interested.wvs<-with(india.fb.wvs[india.fb.wvs$sample==' WVS',],lm(I((interested-1)/3)~1))
mean.india.interested.fb<-with(india.fb.wvs[india.fb.wvs$sample=='Facebook',],lm(I((interested-1)/3)~1))
mean.india.interested.mt<-with(india.mt.wvs[india.mt.wvs$sample=='MTurk',],lm(I((interested-1)/3)~1))
mean.india.interested.qt<-with(india.qt.wvs[india.qt.wvs$sample=='Qualtrics',],lm(I((interested-1)/3)~1))

mean.india.interested.fb.wtd<-svyglm(I((interested-1)/3) ~1, design= india.fb.design)
mean.india.interested.mt.wtd<-svyglm(I((interested-1)/3) ~1, design= india.mt.design)
mean.india.interested.qt.wtd<-svyglm(I((interested-1)/3) ~1, design= india.qt.design)

sd.india.interested.wvs<-with(india.fb.wvs[india.fb.wvs$sample==' WVS',],sd(I((interested-1)/3),na.rm=T))

# Internet news
mean.india.internet_news.nes<-with(india.fb.nes[india.fb.nes$sample==' NES',],lm(I((internet_news-1)/3)~1))
mean.india.internet_news.fb<-with(india.fb.nes[india.fb.nes$sample=='Facebook',],lm(I((internet_news-1)/3)~1))
mean.india.internet_news.mt<-with(india.mt.nes[india.mt.nes$sample=='MTurk',],lm(I((internet_news-1)/3)~1))
mean.india.internet_news.qt<-with(india.qt.nes[india.qt.nes$sample=='Qualtrics',],lm(I((internet_news-1)/3)~1))

mean.india.internet_news.fb.wtd<-svyglm(I((internet_news-1)/3) ~1, design= india.fb.design)
mean.india.internet_news.mt.wtd<-svyglm(I((internet_news-1)/3) ~1, design= india.mt.design)
mean.india.internet_news.qt.wtd<-svyglm(I((internet_news-1)/3) ~1, design= india.qt.design)

sd.india.internet_news.nes<-with(india.fb.nes[india.fb.nes$sample==' NES',],sd(I((internet_news-1)/3),na.rm=T))

# TV news
mean.india.tv_news.nes<-with(india.fb.nes[india.fb.nes$sample==' NES',],lm(I((tv_news-1)/3)~1))
mean.india.tv_news.fb<-with(india.fb.nes[india.fb.nes$sample=='Facebook',],lm(I((tv_news-1)/3)~1))
mean.india.tv_news.mt<-with(india.mt.nes[india.mt.nes$sample=='MTurk',],lm(I((tv_news-1)/3)~1))
mean.india.tv_news.qt<-with(india.qt.nes[india.qt.nes$sample=='Qualtrics',],lm(I((tv_news-1)/3)~1))

mean.india.tv_news.fb.wtd<-svyglm(I((tv_news-1)/3) ~1, design= india.fb.design)
mean.india.tv_news.mt.wtd<-svyglm(I((tv_news-1)/3) ~1, design= india.mt.design)
mean.india.tv_news.qt.wtd<-svyglm(I((tv_news-1)/3) ~1, design= india.qt.design)

sd.india.tv_news.nes<-with(india.fb.nes[india.fb.nes$sample==' NES',],sd(I((tv_news-1)/3),na.rm=T))

# Radio news
mean.india.radio_news.nes<-with(india.fb.nes[india.fb.nes$sample==' NES',],lm(I((radio_news-1)/3)~1))
mean.india.radio_news.fb<-with(india.fb.nes[india.fb.nes$sample=='Facebook',],lm(I((radio_news-1)/3)~1))
mean.india.radio_news.mt<-with(india.mt.nes[india.mt.nes$sample=='MTurk',],lm(I((radio_news-1)/3)~1))
mean.india.radio_news.qt<-with(india.qt.nes[india.qt.nes$sample=='Qualtrics',],lm(I((radio_news-1)/3)~1))

mean.india.radio_news.fb.wtd<-svyglm(I((radio_news-1)/3) ~1, design= india.fb.design)
mean.india.radio_news.mt.wtd<-svyglm(I((radio_news-1)/3) ~1, design= india.mt.design)
mean.india.radio_news.qt.wtd<-svyglm(I((radio_news-1)/3) ~1, design= india.qt.design)

sd.india.radio_news.nes<-with(india.fb.nes[india.fb.nes$sample==' NES',],sd(I((radio_news-1)/3),na.rm=T))

# Newspaper news
mean.india.paper_news.nes<-with(india.fb.nes[india.fb.nes$sample==' NES',],lm(I((paper_news-1)/3)~1))
mean.india.paper_news.fb<-with(india.fb.nes[india.fb.nes$sample=='Facebook',],lm(I((paper_news-1)/3)~1))
mean.india.paper_news.mt<-with(india.mt.nes[india.mt.nes$sample=='MTurk',],lm(I((paper_news-1)/3)~1))
mean.india.paper_news.qt<-with(india.qt.nes[india.qt.nes$sample=='Qualtrics',],lm(I((paper_news-1)/3)~1))

mean.india.paper_news.fb.wtd<-svyglm(I((paper_news-1)/3) ~1, design= india.fb.design)
mean.india.paper_news.mt.wtd<-svyglm(I((paper_news-1)/3) ~1, design= india.mt.design)
mean.india.paper_news.qt.wtd<-svyglm(I((paper_news-1)/3) ~1, design= india.qt.design)

sd.india.paper_news.nes<-with(india.fb.nes[india.fb.nes$sample==' NES',],sd(I((paper_news-1)/3),na.rm=T))

# Know chief minister
mean.india.know_cm.nes<-with(india.fb.nes[india.fb.nes$sample==' NES',],lm(know_cm~1))
mean.india.know_cm.fb<-with(india.fb.nes[india.fb.nes$sample=='Facebook',],lm(know_cm~1))
mean.india.know_cm.mt<-with(india.mt.nes[india.mt.nes$sample=='MTurk',],lm(know_cm~1))
mean.india.know_cm.qt<-with(india.qt.nes[india.qt.nes$sample=='Qualtrics',],lm(know_cm~1))

mean.india.know_cm.fb.wtd<-svyglm(know_cm ~1, design= india.fb.design)
mean.india.know_cm.mt.wtd<-svyglm(know_cm ~1, design= india.mt.design)
mean.india.know_cm.qt.wtd<-svyglm(know_cm ~1, design= india.qt.design)

sd.india.know_cm.nes<-with(india.fb.nes[india.fb.nes$sample==' NES',],sd(know_cm,na.rm=T))

# Know Republic Day
mean.india.know_republic.sons<-with(india.fb.sons[india.fb.sons$sample==' SONS',],lm(know_republic~1))
mean.india.know_republic.fb<-with(india.fb.sons[india.fb.sons$sample=='Facebook',],lm(know_republic~1))
mean.india.know_republic.mt<-with(india.mt.sons[india.mt.sons$sample=='MTurk',],lm(know_republic~1))
mean.india.know_republic.qt<-with(india.qt.sons[india.qt.sons$sample=='Qualtrics',],lm(know_republic~1))

mean.india.know_republic.fb.wtd<-svyglm(know_republic ~1, design= india.fb.design)
mean.india.know_republic.mt.wtd<-svyglm(know_republic ~1, design= india.mt.design)
mean.india.know_republic.qt.wtd<-svyglm(know_republic ~1, design= india.qt.design)

sd.india.know_republic.sons<-with(india.fb.sons[india.fb.sons$sample==' SONS',],sd(know_republic,na.rm=T))

# Know Gandhi's birthday
mean.india.know_gandhi.sons<-with(india.fb.sons[india.fb.sons$sample==' SONS',],lm(know_gandhi~1))
mean.india.know_gandhi.fb<-with(india.fb.sons[india.fb.sons$sample=='Facebook',],lm(know_gandhi~1))
mean.india.know_gandhi.mt<-with(india.mt.sons[india.mt.sons$sample=='MTurk',],lm(know_gandhi~1))
mean.india.know_gandhi.qt<-with(india.qt.sons[india.qt.sons$sample=='Qualtrics',],lm(know_gandhi~1))

mean.india.know_gandhi.fb.wtd<-svyglm(know_gandhi ~1, design= india.fb.design)
mean.india.know_gandhi.mt.wtd<-svyglm(know_gandhi ~1, design= india.mt.design)
mean.india.know_gandhi.qt.wtd<-svyglm(know_gandhi ~1, design= india.qt.design)

sd.india.know_gandhi.sons<-with(india.fb.sons[india.fb.sons$sample==' SONS',],sd(know_gandhi,na.rm=T))

# Gather coefficients

mean.india.pol1.rep<-sapply(list(mean.india.ideol.wvs, mean.india.partyid.nes, mean.india.interested.wvs, mean.india.registered.nes09, mean.india.voted.nes, mean.india.vote_bjp.nes, mean.india.vote_inc.nes, mean.india.vote_other.nes),coef)
mean.india.pol1.fb<-sapply(list(mean.india.ideol.fb, mean.india.partyid.fb, mean.india.interested.fb, mean.india.registered.fb, mean.india.voted.fb, mean.india.vote_bjp.fb, mean.india.vote_inc.fb, mean.india.vote_other.fb),coef)
mean.india.pol1.mt<-sapply(list(mean.india.ideol.mt, mean.india.partyid.mt, mean.india.interested.mt, mean.india.registered.mt, mean.india.voted.mt, mean.india.vote_bjp.mt, mean.india.vote_inc.mt, mean.india.vote_other.mt),coef)
mean.india.pol1.qt<-sapply(list(mean.india.ideol.qt, mean.india.partyid.qt, mean.india.interested.qt, mean.india.registered.qt, mean.india.voted.qt, mean.india.vote_bjp.qt, mean.india.vote_inc.qt, mean.india.vote_other.qt),coef)

mean.india.pol2.rep<-sapply(list(mean.india.internet_news.nes, mean.india.tv_news.nes, mean.india.radio_news.nes, mean.india.paper_news.nes, mean.india.know_cm.nes, mean.india.know_republic.sons, mean.india.know_gandhi.sons),coef)
mean.india.pol2.fb<-sapply(list(mean.india.internet_news.fb, mean.india.tv_news.fb, mean.india.radio_news.fb, mean.india.paper_news.fb, mean.india.know_cm.fb, mean.india.know_republic.fb, mean.india.know_gandhi.fb),coef)
mean.india.pol2.mt<-sapply(list(mean.india.internet_news.mt, mean.india.tv_news.mt, mean.india.radio_news.mt, mean.india.paper_news.mt, mean.india.know_cm.mt, mean.india.know_republic.mt, mean.india.know_gandhi.mt),coef)
mean.india.pol2.qt<-sapply(list(mean.india.internet_news.qt, mean.india.tv_news.qt, mean.india.radio_news.qt, mean.india.paper_news.qt, mean.india.know_cm.qt, mean.india.know_republic.qt, mean.india.know_gandhi.qt),coef)

mean.india.pol1.fb.wtd<-sapply(list(mean.india.ideol.fb.wtd, mean.india.partyid.fb.wtd, mean.india.interested.fb.wtd, mean.india.registered.fb.wtd, mean.india.voted.fb.wtd, mean.india.vote_bjp.fb.wtd, mean.india.vote_inc.fb.wtd, mean.india.vote_other.fb.wtd),coef)
mean.india.pol1.mt.wtd<-sapply(list(mean.india.ideol.mt.wtd, mean.india.partyid.mt.wtd, mean.india.interested.mt.wtd, mean.india.registered.mt.wtd, mean.india.voted.mt.wtd, mean.india.vote_bjp.mt.wtd, mean.india.vote_inc.mt.wtd, mean.india.vote_other.mt.wtd),coef)
mean.india.pol1.qt.wtd<-sapply(list(mean.india.ideol.qt.wtd, mean.india.partyid.qt.wtd, mean.india.interested.qt.wtd, mean.india.registered.qt.wtd, mean.india.voted.qt.wtd, mean.india.vote_bjp.qt.wtd, mean.india.vote_inc.qt.wtd, mean.india.vote_other.qt.wtd),coef)
mean.india.pol2.fb.wtd<-sapply(list(mean.india.internet_news.fb.wtd, mean.india.tv_news.fb.wtd, mean.india.radio_news.fb.wtd, mean.india.paper_news.fb.wtd, mean.india.know_cm.fb.wtd, mean.india.know_republic.fb.wtd, mean.india.know_gandhi.fb.wtd),coef)
mean.india.pol2.mt.wtd<-sapply(list(mean.india.internet_news.mt.wtd, mean.india.tv_news.mt.wtd, mean.india.radio_news.mt.wtd, mean.india.paper_news.mt.wtd, mean.india.know_cm.mt.wtd, mean.india.know_republic.mt.wtd, mean.india.know_gandhi.mt.wtd),coef)
mean.india.pol2.qt.wtd<-sapply(list(mean.india.internet_news.qt.wtd, mean.india.tv_news.qt.wtd, mean.india.radio_news.qt.wtd, mean.india.paper_news.qt.wtd, mean.india.know_cm.qt.wtd, mean.india.know_republic.qt.wtd, mean.india.know_gandhi.qt.wtd),coef)

se.india.pol1.rep<-sapply(list(mean.india.ideol.wvs, mean.india.partyid.nes, mean.india.interested.wvs, mean.india.registered.nes09, mean.india.voted.nes, mean.india.vote_bjp.nes, mean.india.vote_inc.nes, mean.india.vote_other.nes),function(x) coef(summary(x))[2])
se.india.pol1.fb<-sapply(list(mean.india.ideol.fb, mean.india.partyid.fb, mean.india.interested.fb, mean.india.registered.fb, mean.india.voted.fb, mean.india.vote_bjp.fb, mean.india.vote_inc.fb, mean.india.vote_other.fb),function(x) coef(summary(x))[2])
se.india.pol1.mt<-sapply(list(mean.india.ideol.mt, mean.india.partyid.mt, mean.india.interested.mt, mean.india.registered.mt, mean.india.voted.mt, mean.india.vote_bjp.mt, mean.india.vote_inc.mt, mean.india.vote_other.mt),function(x) coef(summary(x))[2])
se.india.pol1.qt<-sapply(list(mean.india.ideol.qt, mean.india.partyid.qt, mean.india.interested.qt, mean.india.registered.qt, mean.india.voted.qt, mean.india.vote_bjp.qt, mean.india.vote_inc.qt, mean.india.vote_other.qt),function(x) coef(summary(x))[2])

se.india.pol2.rep<-sapply(list(mean.india.internet_news.nes, mean.india.tv_news.nes, mean.india.radio_news.nes, mean.india.paper_news.nes, mean.india.know_cm.nes, mean.india.know_republic.sons, mean.india.know_gandhi.sons),function(x) coef(summary(x))[2])
se.india.pol2.fb<-sapply(list(mean.india.internet_news.fb, mean.india.tv_news.fb, mean.india.radio_news.fb, mean.india.paper_news.fb, mean.india.know_cm.fb, mean.india.know_republic.fb, mean.india.know_gandhi.fb),function(x) coef(summary(x))[2])
se.india.pol2.mt<-sapply(list(mean.india.internet_news.mt, mean.india.tv_news.mt, mean.india.radio_news.mt, mean.india.paper_news.mt, mean.india.know_cm.mt, mean.india.know_republic.mt, mean.india.know_gandhi.mt),function(x) coef(summary(x))[2])
se.india.pol2.qt<-sapply(list(mean.india.internet_news.qt, mean.india.tv_news.qt, mean.india.radio_news.qt, mean.india.paper_news.qt, mean.india.know_cm.qt, mean.india.know_republic.qt, mean.india.know_gandhi.qt),function(x) coef(summary(x))[2])

se.india.pol1.fb.wtd<-sapply(list(mean.india.ideol.fb.wtd, mean.india.partyid.fb.wtd, mean.india.interested.fb.wtd, mean.india.registered.fb.wtd, mean.india.voted.fb.wtd, mean.india.vote_bjp.fb.wtd, mean.india.vote_inc.fb.wtd, mean.india.vote_other.fb.wtd),function(x) coef(summary(x))[2])
se.india.pol1.mt.wtd<-sapply(list(mean.india.ideol.mt.wtd, mean.india.partyid.mt.wtd, mean.india.interested.mt.wtd, mean.india.registered.mt.wtd, mean.india.voted.mt.wtd, mean.india.vote_bjp.mt.wtd, mean.india.vote_inc.mt.wtd, mean.india.vote_other.mt.wtd),function(x) coef(summary(x))[2])
se.india.pol1.qt.wtd<-sapply(list(mean.india.ideol.qt.wtd, mean.india.partyid.qt.wtd, mean.india.interested.qt.wtd, mean.india.registered.qt.wtd, mean.india.voted.qt.wtd, mean.india.vote_bjp.qt.wtd, mean.india.vote_inc.qt.wtd, mean.india.vote_other.qt.wtd),function(x) coef(summary(x))[2])
se.india.pol2.fb.wtd<-sapply(list(mean.india.internet_news.fb.wtd, mean.india.tv_news.fb.wtd, mean.india.radio_news.fb.wtd, mean.india.paper_news.fb.wtd, mean.india.know_cm.fb.wtd, mean.india.know_republic.fb.wtd, mean.india.know_gandhi.fb.wtd),function(x) coef(summary(x))[2])
se.india.pol2.mt.wtd<-sapply(list(mean.india.internet_news.mt.wtd, mean.india.tv_news.mt.wtd, mean.india.radio_news.mt.wtd, mean.india.paper_news.mt.wtd, mean.india.know_cm.mt.wtd, mean.india.know_republic.mt.wtd, mean.india.know_gandhi.mt.wtd),function(x) coef(summary(x))[2])
se.india.pol2.qt.wtd<-sapply(list(mean.india.internet_news.qt.wtd, mean.india.tv_news.qt.wtd, mean.india.radio_news.qt.wtd, mean.india.paper_news.qt.wtd, mean.india.know_cm.qt.wtd, mean.india.know_republic.qt.wtd, mean.india.know_gandhi.qt.wtd),function(x) coef(summary(x))[2])

diff.india.pol1.fb<-abs(mean.india.pol1.fb-mean.india.pol1.rep)
diff.india.pol1.mt<-abs(mean.india.pol1.mt-mean.india.pol1.rep)
diff.india.pol1.qt<-abs(mean.india.pol1.qt-mean.india.pol1.rep)
which.closest<-apply(cbind(diff.india.pol1.fb,diff.india.pol1.mt,diff.india.pol1.qt),1, which.min)
col.india.pol1.fb<-ifelse(which.closest==1,'red','black')
col.india.pol1.mt<-ifelse(which.closest==2,'red','black')
col.india.pol1.qt<-ifelse(which.closest==3,'red','black')

diff.india.pol2.fb<-abs(mean.india.pol2.fb-mean.india.pol2.rep)
diff.india.pol2.mt<-abs(mean.india.pol2.mt-mean.india.pol2.rep)
diff.india.pol2.qt<-abs(mean.india.pol2.qt-mean.india.pol2.rep)
which.closest<-apply(cbind(diff.india.pol2.fb,diff.india.pol2.mt,diff.india.pol2.qt),1, which.min)
col.india.pol2.fb<-ifelse(which.closest==1,'red','black')
col.india.pol2.mt<-ifelse(which.closest==2,'red','black')
col.india.pol2.qt<-ifelse(which.closest==3,'red','black')

sd.india.pol.rep<-c(sd.india.ideol.wvs, sd.india.partyid.nes, sd.india.interested.wvs, sd.india.registered.nes09, sd.india.voted.nes, sd.india.vote_bjp.nes, sd.india.vote_inc.nes, sd.india.vote_other.nes, sd.india.internet_news.nes, sd.india.tv_news.nes, sd.india.radio_news.nes, sd.india.paper_news.nes, sd.india.know_cm.nes, sd.india.know_republic.sons, sd.india.know_gandhi.sons)

std.diff.pol.india.fb<-(c(mean.india.pol1.fb,mean.india.pol2.fb)-c(mean.india.pol1.rep,mean.india.pol2.rep))/sd.india.pol.rep
std.diff.pol.india.mt<-(c(mean.india.pol1.mt,mean.india.pol2.mt)-c(mean.india.pol1.rep,mean.india.pol2.rep))/sd.india.pol.rep
std.diff.pol.india.qt<-(c(mean.india.pol1.qt,mean.india.pol2.qt)-c(mean.india.pol1.rep,mean.india.pol2.rep))/sd.india.pol.rep
std.diff.pol.india.fb.wtd<-(c(mean.india.pol1.fb.wtd,mean.india.pol2.fb.wtd)-c(mean.india.pol1.rep,mean.india.pol2.rep))/sd.india.pol.rep
std.diff.pol.india.mt.wtd<-(c(mean.india.pol1.mt.wtd,mean.india.pol2.mt.wtd)-c(mean.india.pol1.rep,mean.india.pol2.rep))/sd.india.pol.rep
std.diff.pol.india.qt.wtd<-(c(mean.india.pol1.qt.wtd,mean.india.pol2.qt.wtd)-c(mean.india.pol1.rep,mean.india.pol2.rep))/sd.india.pol.rep

# Standardized difference table (Appendix Table 11)

india.stddiff.pol.table<-round(data.frame(fb= std.diff.pol.india.fb, fb.wtd= std.diff.pol.india.fb.wtd, mt= std.diff.pol.india.mt, mt.wtd= std.diff.pol.india.mt.wtd, qt= std.diff.pol.india.qt, qt.wtd= std.diff.pol.india.qt.wtd),2)
rownames(india.stddiff.pol.table)<-c('Left--Right (1--10)','Any party ID','Political interest (1--4)','Registered to Vote','Turnout 2014','BJP','INC','Other','Internet','Television','Radio','Newspaper','State Chief Minister','Republic Day',"Gandhi's Birthday")
colnames(india.stddiff.pol.table)<-c('Facebook','Facebook\nWeighted','MTurk','MTurk\nWeighted','Qualtrics','Qualtrics\nWeighted')

india.stddiff.pol.table.note<-'NOTE: Entries are mean differences between the convenience and probability sample figures listed in Table \\ref{india.pol.table}, divided by the standard deviation in the probability sample.'

india.stddiff.pol.table.latex<-latex(india.stddiff.pol.table,file='india_stddiff_pol_table.tex', rowlabel = '', collabel.just=rep('c',ncol(india.stddiff.pol.table)), col.just=rep('c',ncol(india.stddiff.pol.table)), caption = 'India Political Variables: Standardized Difference from Probability Samples', extracolsize='normalsize', insert.bottom= india.stddiff.pol.table.note, booktabs = F, ctable = T, where = "htp")

# Plot (Main Text Figure 6)

lwd<-1.5
adjust<-0.15
nvars<-length(mean.india.pol1.rep)

pdf(file='india_pol1.pdf',width=10,height=7)
par(mfrow=c(1,2))

par(mar=c(3,6,2,0)+.01)
plot(mean.india.pol1.rep, nvars:1+1.5*adjust, type='p',axes=F,frame.plot=T,xlab='',ylab='',pch=19,xlim=0:1,ylim=c(1-1.5*adjust,nvars+1.5*adjust),main='Unweighted',sub='')
Axis(side=1,at=seq(0,1,.25),labels= seq(0,1,.25))
Axis(side=2,at=c((nvars+1):0),labels=c('','Left-Right','Any Party ID','Political\nInterest', 'Registered\nto Vote', 'Turnout 2014','Voted BJP','Voted INC','Voted Other',''),las=1)
abline(h=1:(nvars-1)+.5,lwd=1,lty=3)
segments(mean.india.pol1.rep-qnorm(.975)*se.india.pol1.rep,nvars:1+1.5*adjust, mean.india.pol1.rep+qnorm(.975)*se.india.pol1.rep,nvars:1+1.5*adjust,lwd=lwd)
segments(mean.india.pol1.fb-qnorm(.975)*se.india.pol1.fb,nvars:1+0.5*adjust, mean.india.pol1.fb+qnorm(.975)*se.india.pol1.fb,nvars:1+0.5*adjust,lwd=lwd)
segments(mean.india.pol1.mt-qnorm(.975)*se.india.pol1.mt,nvars:1-0.5*adjust, mean.india.pol1.mt+qnorm(.975)*se.india.pol1.mt,nvars:1-0.5*adjust,lwd=lwd)
segments(mean.india.pol1.qt-qnorm(.975)*se.india.pol1.qt,nvars:1-1.5*adjust, mean.india.pol1.qt+qnorm(.975)*se.india.pol1.qt,nvars:1-1.5*adjust,lwd=lwd)
points(mean.india.pol1.fb, nvars:1+0.5*adjust, pch=21, bg='white')
points(mean.india.pol1.mt, nvars:1-0.5*adjust, pch=8)
points(mean.india.pol1.qt, nvars:1-1.5*adjust, pch=25, bg='white')

par(mar=c(3,2,2,4)+.01)
plot(mean.india.pol1.rep, nvars:1+1.5*adjust, type='p',axes=F,frame.plot=T,xlab='',ylab='',pch=19,xlim=0:1,ylim=c(1-1.5*adjust,nvars+1.5*adjust),main='Weighted',sub='')
Axis(side=1,at=seq(0,1,.25),labels= seq(0,1,.25))
abline(h=1:(nvars-1)+.5,lwd=1,lty=3)
segments(mean.india.pol1.rep-qnorm(.975)*se.india.pol1.rep,nvars:1+1.5*adjust, mean.india.pol1.rep+qnorm(.975)*se.india.pol1.rep,nvars:1+1.5*adjust,lwd=lwd)
segments(mean.india.pol1.fb.wtd-qnorm(.975)*se.india.pol1.fb.wtd,nvars:1+0.5*adjust, mean.india.pol1.fb.wtd+qnorm(.975)*se.india.pol1.fb.wtd,nvars:1+0.5*adjust,lwd=lwd)
segments(mean.india.pol1.mt.wtd-qnorm(.975)*se.india.pol1.mt.wtd,nvars:1-0.5*adjust, mean.india.pol1.mt.wtd+qnorm(.975)*se.india.pol1.mt.wtd,nvars:1-0.5*adjust,lwd=lwd)
segments(mean.india.pol1.qt.wtd-qnorm(.975)*se.india.pol1.qt.wtd,nvars:1-1.5*adjust, mean.india.pol1.qt.wtd+qnorm(.975)*se.india.pol1.qt.wtd,nvars:1-1.5*adjust,lwd=lwd)
points(mean.india.pol1.fb.wtd, nvars:1+0.5*adjust, pch=21, bg='white')
points(mean.india.pol1.mt.wtd, nvars:1-0.5*adjust, pch=8)
points(mean.india.pol1.qt.wtd, nvars:1-1.5*adjust, pch=25, bg='white')
legend('bottomright',pch=c(19,21,8,25),legend=c('Probability','Facebook','MTurk','Qualtrics'),title='Sample',bg='white')

dev.off()

# Plot (Main Text Figure 7)

lwd<-1.5
adjust<-0.15
nvars<-length(mean.india.pol2.rep)

pdf(file='india_pol2.pdf',width=10,height=7)
par(mfrow=c(1,2))

par(mar=c(3,7,2,0)+.01)
plot(mean.india.pol2.rep, nvars:1+1.5*adjust, type='p',axes=F,frame.plot=T,xlab='',ylab='',pch=19,xlim=0:1,ylim=c(1-1.5*adjust,nvars+1.5*adjust),main='Unweighted',sub='')
Axis(side=1,at=seq(0,1,.25),labels= seq(0,1,.25))
Axis(side=2,at=c((nvars+1):0),labels=c('','Internet','Television','Radio', 'Newspaper', 'Chief\nMinister','Republic\nDay',"Gandhi's\nBirthday",''),las=1)
Axis(side=2,at=c(2,5.5),labels=c('Political Knowledge','News Exposure'), line=4.5,tick=F,font=2)
abline(h=1:(nvars-1)+.5,lwd=1,lty=3)
segments(mean.india.pol2.rep-qnorm(.975)*se.india.pol2.rep,nvars:1+1.5*adjust, mean.india.pol2.rep+qnorm(.975)*se.india.pol2.rep,nvars:1+1.5*adjust,lwd=lwd)
segments(mean.india.pol2.fb-qnorm(.975)*se.india.pol2.fb,nvars:1+0.5*adjust, mean.india.pol2.fb+qnorm(.975)*se.india.pol2.fb,nvars:1+0.5*adjust,lwd=lwd)
segments(mean.india.pol2.mt-qnorm(.975)*se.india.pol2.mt,nvars:1-0.5*adjust, mean.india.pol2.mt+qnorm(.975)*se.india.pol2.mt,nvars:1-0.5*adjust,lwd=lwd)
segments(mean.india.pol2.qt-qnorm(.975)*se.india.pol2.qt,nvars:1-1.5*adjust, mean.india.pol2.qt+qnorm(.975)*se.india.pol2.qt,nvars:1-1.5*adjust,lwd=lwd)
points(mean.india.pol2.fb, nvars:1+0.5*adjust, pch=21, bg='white')
points(mean.india.pol2.mt, nvars:1-0.5*adjust, pch=8)
points(mean.india.pol2.qt, nvars:1-1.5*adjust, pch=25, bg='white')

par(mar=c(3,2,2,5)+.01)
plot(mean.india.pol2.rep, nvars:1+1.5*adjust, type='p',axes=F,frame.plot=T,xlab='',ylab='',pch=19,xlim=0:1,ylim=c(1-1.5*adjust,nvars+1.5*adjust),main='Weighted',sub='')
Axis(side=1,at=seq(0,1,.25),labels= seq(0,1,.25))
abline(h=1:(nvars-1)+.5,lwd=1,lty=3)
segments(mean.india.pol2.rep-qnorm(.975)*se.india.pol2.rep,nvars:1+1.5*adjust, mean.india.pol2.rep+qnorm(.975)*se.india.pol2.rep,nvars:1+1.5*adjust,lwd=lwd)
segments(mean.india.pol2.fb.wtd-qnorm(.975)*se.india.pol2.fb.wtd,nvars:1+0.5*adjust, mean.india.pol2.fb.wtd+qnorm(.975)*se.india.pol2.fb.wtd,nvars:1+0.5*adjust,lwd=lwd)
segments(mean.india.pol2.mt.wtd-qnorm(.975)*se.india.pol2.mt.wtd,nvars:1-0.5*adjust, mean.india.pol2.mt.wtd+qnorm(.975)*se.india.pol2.mt.wtd,nvars:1-0.5*adjust,lwd=lwd)
segments(mean.india.pol2.qt.wtd-qnorm(.975)*se.india.pol2.qt.wtd,nvars:1-1.5*adjust, mean.india.pol2.qt.wtd+qnorm(.975)*se.india.pol2.qt.wtd,nvars:1-1.5*adjust,lwd=lwd)
points(mean.india.pol2.fb.wtd, nvars:1+0.5*adjust, pch=21, bg='white')
points(mean.india.pol2.mt.wtd, nvars:1-0.5*adjust, pch=8)
points(mean.india.pol2.qt.wtd, nvars:1-1.5*adjust, pch=25, bg='white')
legend('bottomleft',pch=c(19,21,8,25),legend=c('Probability','Facebook','MTurk','Qualtrics'),title='Sample',bg='white')
dev.off()
